【问题标题】:Is changing the table height by changing its contrains good practice?通过改变约束来改变桌子高度是一种好的做法吗?
【发布时间】:2021-10-17 08:42:56
【问题描述】:

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

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


    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)
    }
}

【问题讨论】:

    标签: ios swift xcode uitableview


    【解决方案1】:

    我要注意避免您遇到的问题,

    • 尽量避免尺寸的任何常量或值。
    • 使用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

    请检查以下审查的 sn-p 是否有效。

       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)
        }
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-12
      • 2018-01-10
      • 2012-11-06
      • 2010-11-26
      • 1970-01-01
      • 2013-04-05
      • 2011-08-06
      • 1970-01-01
      相关资源
      最近更新 更多