【发布时间】: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