ios - 在 UITableview 中删除一行时在底部插入新行
问题描述
我有一个要求,我需要始终显示一个三行的表格视图。
当用户滑动删除一行时,我需要在底部添加一个新行。
发生这种情况时,必须保留将行向左移动以删除的滑动动画。我的意思是删除动画不应该受到影响。这可能吗?
解决方案
首先,您的方法dataSource
必须始终返回 3 numberOfRows(in:)
。然后您可以通过这种方式提交更改:
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
相应地更新其余部分,因为单元格可能会被重复使用。代码需要在call之前performBatchUpdates
添加。像这样的东西:
var cellsText = ["A","B","C"]
// Inside your tableView(:commit:forRowAt) method:
if editingStyle == .delete {
cellsText.remove(at: indexPath.row)
cellsText.append("D")
// ...
}
推荐阅读
- computer-vision - Azure ComputerVision OCR 和 PDF 格式
- python-2.7 - 需要安装/启动 pgadmin4 SQLAlchemy 1.1.0 或更高版本时出错
- r - 如何在映射之前访问列表的名称?
- angular - 用于角度反应形式的动态对象属性名称
- matlab - 根据 z 轴上的条件以两种颜色绘制曲面图
- user-interface - VAADIN:UI.access() 推送队列的大小
- arrays - 在python中格式化字符串以转换为num数组
- c# - C# WPF 多窗口
- microchip - PIC32MZ NVM 写操作不正确
- python-3.x - 我们如何将文件(作为 Multipart 请求的一部分接受)发送到 python 中的 MINIO 对象存储而不将文件保存在本地存储中?