【发布时间】:2019-01-27 19:20:44
【问题描述】:
When deleting rows from the bottom
在我的 TableView 中删除一行或多行后,TableView 单元格似乎会以一种奇怪的方式移动或刷新,从而创建多个空白行。似乎从屏幕外的行开始。
我已尝试使用 beginUpdates、endUpdates 和 performBatchUpdates,但行为没有改变。我还确认数据源数组正在正确更新,tableview 中的行数也是如此。
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return paymentsArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! UserPaymentCell
let payment = paymentsArray[indexPath.row]
cell.payment = payment
cell.selectionStyle = .none
cell.preservesSuperviewLayoutMargins = false
cell.separatorInset = UIEdgeInsets(top: 0, left: 75, bottom: 0, right: 0)
cell.layoutMargins = UIEdgeInsets.zero
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
let payment = paymentsArray[indexPath.row]
if payment.payerUID == Auth.auth().currentUser?.uid {
return true
} else {
return false
}
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
let payment = paymentsArray[indexPath.row]
switch editingStyle {
case .delete:
deleteBillAndRefreshTotals(bill: payment, indexPath: indexPath)
default:
return
}
}
func deleteBillAndRefreshTotals(bill: Bill, indexPath: IndexPath) {
print("DELETING CELL")
paymentsArray.remove(at: indexPath.row)
paymentsTableView.deleteRows(at: [indexPath], with: .automatic)
print(paymentsTableView.numberOfRows(inSection: 0))
}
预期结果 - 要删除的行以及已删除单元格上方或下方的所有单元格一起移动。
【问题讨论】:
-
可以看看数据源方法吗?
-
@matt 为你包含了数据源方法!
-
嗯,我看不出有什么问题,除了
deleteRows应该包裹在performBatchUpdates中。如果这不能解决它,您可以发布一个演示该问题的小型测试项目吗?基本上它应该只包含您已经发布的代码。 -
顺便说一下,这是我的可下载示例项目,您可以看到它没有显示此问题。 github.com/mattneub/Programming-iOS-Book-Examples/tree/master/…
标签: ios swift uitableview tableview