是否通过改变其约束良好做法来改变 table 高度?

Is changing the table height by changing its contrains good practice?

我有一个不可滚动的 TableView。我希望它的高度由条目数决定。 为了完成这项工作,我在代码中使用了 TableView 的高度约束 (= CGFloat(data.count) * tableView.rowheight)。这是好的做法吗?

我注意到当我删除 TableView 的最后一行时,它或多或少会跳过动画。这是因为 table 的高度调整得比动画需要的快吗? 我该如何解决这个问题?


    var data = [1,2,3,4,5,6,7,8,9,10]

    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var tableViewHeight: NSLayoutConstraint!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.rowHeight = 50
        tableViewHeight.constant = CGFloat(data.count) * tableView.rowHeight
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = "\(data[indexPath.row])"
        return cell
    }
    
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            data.remove(at: indexPath.row)
            tableView.deleteRows(at: [indexPath], with: .automatic)
            self.tableViewHeight.constant = CGFloat(self.data.count) * self.tableView.rowHeight
        }
    }
    
    override func setEditing(_ editing: Bool, animated: Bool) {
        super.setEditing(!isEditing, animated: true)
        tableView.setEditing(!tableView.isEditing, animated: true)
    }
}

为了避免您遇到的问题,我注意事项,

  • 尽量避免维度的任何常量或值。
  • 使用 constraints 并尝试在一个地方定义它(StoryBoard、Xib 或在代码中)。

因为它的高度是由它的内容附加到屏幕边缘来决定的,这是行不通的。

关于您的问题,

  • 尝试检查故事板和代码中是否出现任何冲突约束。就像在故事板中定义了整个 tableview 一样,为什么不 tableView.rowHeight = 50 也保留在那里。
  • 删除时,tableView.deleteRows().autometic 个动画。根据文档,case automatic = 100 // available in iOS 5.0. chooses an appropriate animation style for you。让我们将其设置为 .none.

请检查以下经过审核的代码段是否有效。

   var data = [1,2,3,4,5,6,7,8,9,10]

    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var tableViewHeight: NSLayoutConstraint!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.rowHeight = 50
        tableViewHeight.constant = CGFloat(data.count) * tableView.rowHeight
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        self.tableViewHeight.constant = CGFloat(self.data.count) * self.tableView.rowHeight
        return data.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = "\(data[indexPath.row])"
        return cell
    }
    
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        // I think this is causing the problem. 
         if editingStyle == .delete {
            data.remove(at: indexPath.row)
            // use no animation
            tableView.deleteRows(at: [indexPath], with: .none)
            // wont update the constraint here.
            // self.tableViewHeight.constant = CGFloat(self.data.count) * self.tableView.rowHeight
        }
    }
    
    override func setEditing(_ editing: Bool, animated: Bool) {
        super.setEditing(!isEditing, animated: true)
        tableView.setEditing(!tableView.isEditing, animated: true)
    }
}

如果还没有,也请检查这些,

  • Change UITableView height dynamically