【问题标题】:Set UITableView's superview height accordingly to the number of table view cells根据表格视图单元格的数量设置 UITableView 的超级视图高度
【发布时间】:2019-03-05 13:39:50
【问题描述】:

我的 UITableView 嵌入在 UIView 中,我希望这个超级视图的高度根据表格视图具有的 UITableViewCells 的数量而变化。

也就是说,如果 tableView 有 1 个单元格,UIView 将是这个单元格的大小,如果有 2 个单元格,UIView 将增长到 2 个单元格的大小。

【问题讨论】:

    标签: swift uitableview


    【解决方案1】:

    我会在UITableViewCellcontentView 中再添加一个UIView,我将用作内容视图。然后我会设置backgroundColor 真正的contentView

    【讨论】:

    • @AamirR 我理解这样的问题:他的UITableView 具有固定高度,他需要更改可见单元格后面的颜色。因此,如果只有一个单元格,则在该单元格后面将是彩色背景。如果有两个单元格,则这两个单元格后面将是彩色背景。
    【解决方案2】:

    如果您使用的是 Autolayout 约束,您只需更新 UITableView 的高度约束本身就可以了。

    以下是基本步骤:

    1. 子类 UITableView:

      class CustomTableView: UITableView {
      
          var maximumTableViewHeight: CGFloat = UIScreen.main.bounds.size.height
      
          private var heightConstraint: NSLayoutConstraint!
      
          override func reloadData() {
             super.reloadData()
              self.heightConstraint.constant = min(self.contentSize.height, maximumTableViewHeight)
          }
      
          override func awakeFromNib() {
              super.awakeFromNib()
      
              self.heightConstraint = NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal, toItem: nil,
                                                         attribute: .notAnAttribute, multiplier: 1, constant: 0)
              self.addConstraint(heightConstraint)
          }
      
      }
      
    2. 具有我们的子类CustomTableView的故事板

    1. 然后你的视图控制器:

      class ViewController: UIViewController, UITableViewDataSource {
      
          @IBOutlet var tableView: CustomTableView!
          private let cellIdentifier = "Cell"
      
          var dataSource: [Int] = [1, 2, 3] {
              didSet {
                  self.tableView.reloadData()
              }
          }
      
          override func viewDidLoad() {
              super.viewDidLoad()
      
              self.tableView.dataSource = self
              self.tableView.maximumTableViewHeight = 400 // Adjust as per your max height
          }
      
          @IBAction func addCell(_ sender: Any) {
              let count = self.dataSource.count + 1
              self.dataSource.append(count)
      
              let scrollToIndexPath = IndexPath(row: count - 1, section: 0)
              DispatchQueue.main.async {
                  self.tableView.scrollToRow(at: scrollToIndexPath, at: .bottom, animated: false)
              }
          }
      
          func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
              return self.dataSource.count
          }
      
          func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
              var cell: UITableViewCell! = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)
              if cell == nil {
                  cell = UITableViewCell(style: .default, reuseIdentifier: cellIdentifier)
              }
      
              cell.textLabel?.text = "Value: " + String(dataSource[indexPath.row])
              return cell
          }
      
      }
      

    产生:

    注意红色区域,这是UITableView的父UIView

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多