[英]Insert new row at the bottom upon deleting a row in UITableview
我有一个需要始终显示三行的tableview的要求。
当用户滑动删除行时,我需要在底部添加新行。
在这种情况下,必须保留将行向左移动以进行删除的滑动动画。 我的意思是删除动画不应该受到影响。 这可能吗?
首先,您的dataSource
必须始终为numberOfRows(in:)
方法返回3。 然后,您可以通过以下方式提交更改:
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
tableView.performBatchUpdates({
// Delete the swiped row
tableView.deleteRows(at: [indexPath], with: .left)
// Get the last row index (numberOfRows-1)
let lastRowIndex = tableView.numberOfRows(inSection: indexPath.section)-1
// Insert a new row at the end
tableView.insertRows(at: [IndexPath(row: lastRowIndex, section: 0)], with: .top)
}, completion: nil)
}
}
不要忘记相应地更新其余的dataSource
,因为单元可能会被重用。 需要在调用performBatchUpdates
之前添加代码。 像这样:
var cellsText = ["A","B","C"]
// Inside your tableView(:commit:forRowAt) method:
if editingStyle == .delete {
cellsText.remove(at: indexPath.row)
cellsText.append("D")
// ...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.