编辑行时,TableView节标题会移动

时间:2016-10-18 21:27:48

标签: ios swift uitableview

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

有关部分标题随单元格移动的原因的任何线索吗?

标题单元在故事板中定义为动态原型单元格,行单元格以单独的xib定义并在tableview中注册。部分单元格在故事板中没有选中"在编辑时缩进" -option。

"每周报告"在节标题中。

Before and after dragging to edit

以下是我为实现编辑而实施的代码:

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)
        }
    }
}

我已尝试在设备和两个不同的模拟器上,并清理构建文件夹。

修改

标题从故事板加载,其中有一个可重复使用的标识符:" WeeklyReportHeader",以及由tableView的委托返回的UIView。

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

1 个答案:

答案 0 :(得分:4)

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

要解决此问题,不要使用单元格本身(并将其作为标题的UIView转换),请使用它的contentView属性,无论如何都是UIView。

例如

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