【发布时间】:2016-10-18 21:27:47
【问题描述】:
我正在构建一个带有 tableView 的 iOS 应用程序,它使用 Realm 数据库作为数据模型。当我尝试通过将单元格向左拖动来删除一行时,节标题会跟随向左拖动的移动。按下删除时,单元格被删除,节标题移回正确的位置。
有任何线索说明部分标题为何随单元格移动?
标题单元格在情节提要中定义为动态原型单元格,行单元格在单独的 xib 中定义并在 tableview 中注册。部分单元格在情节提要中未选中“编辑时缩进”选项。
“每周报告”位于部分标题中。
这是我为启用编辑而实现的代码:
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
try! realm.write {
let reportToDelete = reportList[indexPath.row]
realm.delete(reportToDelete)
tableView.deleteRows(at: [indexPath], with: .fade)
}
}
}
我在设备和两个不同的模拟器上都试过了,并且在 build 文件夹中使用了 clean。
编辑
标题是从情节提要中加载的,它有一个可重复使用的标识符:“WeeklyReportHeader”,而 UIView 则由 tableView 的委托返回。
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return tableView.dequeueReusableCell(withIdentifier: "WeeklyReportHeader")! as UIView
}
【问题讨论】:
-
表头单元格和行单元格是同一类型吗?
-
我的意思是相同的自定义类
-
No.. section header 没有子类,它只是一个 UITableViewCell,而行单元格是 UITableViewCell 的特定子类。
-
好的,我想我有一个解决方案,请稍等。
-
实际上在我把它全部写出来之前,你介意将你用来创建标题的代码添加到你的问题中吗?这将帮助我知道我是否走在正确的轨道上。
标签: ios swift uitableview