ios - Swift 5:在 TableView 中将单元格从一个部分移动到另一部分
问题描述
我正在做一些表格视图练习。我想实现一个函数,通过使用 moveRowAt 方法将一个单元格从一个部分移动到另一个部分。当我在同一部分中移动/排列一个单元格时,它工作正常。但是,当我将一个单元格从一个部分移动到另一个部分时,它会给我一个这样的错误:
这是 moveRowAt 函数:
override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath)
{
// Move data
let todo = todos[fromIndexPath.section][fromIndexPath.row]
todos[fromIndexPath.section].remove(at: fromIndexPath.row)
// move from one section to another section
if to.section != fromIndexPath.section
{
todos[to.section].insert(todo, at: to.row)
// Update View
tableView.beginUpdates()
tableView.moveRow(at: fromIndexPath, to: to)
tableView.endUpdates()
}
else
{
todos[fromIndexPath.section].insert(todo, at: to.row)
// Update View
tableView.beginUpdates()
tableView.moveRow(at: fromIndexPath, to: to)
tableView.endUpdates()
}
}
todos 的声明是:
struct ToDo {
var name = ""
var checked = false
var details: String = ""
}
var todos: [[ToDo]]
感谢您的帮助和分享您的想法!
解决方案
你不应该打电话
tableView.moveRow(at: fromIndexPath, to: to)
自己因为这将导致递归循环只需执行删除和插入
推荐阅读
- flutter - 使用“gmailSaslXoauth2”获取不正确的用户名和密码异常
- html - 根据角度(setvalue)中的先前日期选择器自动设置日期选择器值
- python - 使用for循环在y轴上绘制数据框的多列?
- c++ - 需要有关 pybind11 的帮助
- reactjs - 如何在 unorderlist 中呈现特定的字符串数组
- python - 我无法从这个特定数据集的日期列中提取年份
- java - Logger AOP - 拦截对公共方法的所有调用
- gmail - 如何从 Google Compute Engine 验证 Gmail API?
- c++ - C++ 乘以 BigInt - 在 0x00007FFD777FF530 (ucrtbased.dll) 处引发异常
- mapreduce - Netsuite - 导出的 CSV 文件包含“&”而不是“&”