【问题标题】:UITableView height issueUITableView 高度问题
【发布时间】:2019-02-28 22:37:26
【问题描述】:

我有一个UIStackView,我在其中添加了一个UIButtonUITableView 作为subviews。最初,由于 table view 没有内容,它的高度为零。

当用户采取行动时,UItableView 会收到一些数据并自行重新加载。

numberOfRowsInSection 中,我返回了计数,但由于UITableView 的高度为零,因此对cellForRowAt 的调用不起作用。

我想在没有内容时保持UITableView 的高度为零,并在有内容时给它高度。

由于UITableViewUIStackView 的子视图,它也应该扩展

我怎样才能实现它?

【问题讨论】:

  • 使用 UITableView 的estimatedRowHeight。
  • 这会有什么帮助

标签: ios swift uitableview autolayout uistackview


【解决方案1】:

一旦收到数据,您可以调整 tableview 的框架。检查此链接: UITableView not shown inside UIStackView

【讨论】:

    【解决方案2】:

    嘿,我做了一些与此非常相似的事情,希望这会有所帮助。

    final class SignInVC: UIViewController {
    
        @IBOutlet weak var tableView: UITableView!
        @IBOutlet weak var tableViewHeight: NSLayoutConstraint!
    
        // Whatever max height you are after
        private let maxTableViewHeight = 500
        // Whatever height you are after
        private let rowHeight = 90
    
        // Just using string array for example but use your own array here
        private var fields: [String] = []
    
        override func viewDidLoad() {
            super.viewDidLoad()
            tableView.delegate = self
            tableView.dataSource = self
            setTableViewHeight()
        }
    
        private func setTableViewHeight() {
            var newHeight = fields.count * rowHeight
            if newHeight > maxTableViewHeight {
                newHeight = maxTableViewHeight
            }
            tableViewHeight.constant = newHeight
        }
    
        @IBAction func addField(_ sender: Any) {
            fields.append("Test")
            setTableViewHeight()
            tableView.reloadData()
        }
    
    }
    
    extension SignInVC: UITableViewDelegate, UITableViewDataSource {
    
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return fields.count
        }
    
        // Rest of your datasource/delegate below...
        // I am assuming you have your own already
    
    }
    

    希望这会有所帮助:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-28
      • 1970-01-01
      • 2023-03-10
      • 2015-08-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多