繁体   English   中英

编辑行时,TableView节标题移动

[英]TableView section header moves when a row is edited

我正在使用tableView构建一个iOS应用,它使用Realm数据库作为数据模型。 当我尝试通过向左拖动单元格来删除行时,节标题跟随向左移动。 当按下删除键时,单元格将被删除,并且节标题将移回到正确的位置。

为什么节标题随单元格一起移动的任何线索?

标题单元在情节提要中定义为动态原型单元,行单元定义在单独的xib中并在表格视图中注册。 剖面单元的情节提要中未选中“编辑时缩进”选项。

“每周报告”位于部分标题中。

拖动前后编辑

这是我实现的启用编辑的代码:

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文件夹中进行了清理。

编辑

该标题是从情节提要中加载的,该故事板上具有可重复使用的标识符:“ WeeklyReportHeader”,而UIView由tableView的委托返回。

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    return tableView.dequeueReusableCell(withIdentifier: "WeeklyReportHeader")! as UIView 
}

我认为这与使用单元格作为标头有关,可能与单元重用有关。

要解决此问题,而不是使用单元格本身(并将其强制转换为标题的UIView),请使用它的contentView属性,该属性始终是UIView。

例如

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    return tableView.dequeueReusableCell(withIdentifier: "WeeklyReportHeader")!.contentView
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM