ios - 向使用 Fetch Controller 的 TableView 添加编辑行操作?

标签 ios swift uitableview core-data fetch

我有一个使用核心数据提取来填充自身的表,提取 Controller 存储删除行操作。

但是我也想向该行添加一个编辑功能,我似乎无法将它与我目前拥有的删除功能一起添加,并且需要使用 editActionsForRowAt 来让它出现。

当我使用 editActionsForRowAt 时,它会覆盖现有的 Fetch Controllers 行操作并导致删除不再有效。

知道如何在现有删除旁边添加一个编辑,而不会过度写入或弄乱获取结果 Controller 吗?

这是获取 Controller 的代码,它是表的源及其删除案例和删除功能,我想理想地向其添加一个编辑按钮,而不必用 editActionsForRowAt 覆盖它

func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
        switch (type) {
        case .insert:
            if let indexPath = newIndexPath {
                workoutDesignerTable.insertRows(at: [indexPath], with: .fade)
            }
            break;
        case .delete:
            if let indexPath = indexPath {
                workoutDesignerTable.deleteRows(at: [indexPath], with: .fade)
            }
            break;
        case .update:
            if let indexPath = indexPath, let cell = workoutDesignerTable.cellForRow(at: indexPath) as? RoutineTableViewCell {
                configure(cell, at: indexPath)
            }
            break;
        default:
            print("...")
        }
    }

    // MARK: - DELETING TABLE ROW

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            let UserExercise = fetchedResultsController.managedObjectContext
            UserExercise.delete(self.fetchedResultsController.object(at: indexPath))
            do {
                try UserExercise.save()
            } catch {
                let nserror = error as NSError
                fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
            }
        }
    }

有关详细信息,请查看以下当前表格行编辑操作的屏幕截图。

enter image description here

最佳答案

来自文档tableView(_:editActionsForRowAt:)

Use this method when you want to provide custom actions for one of your table rows. When the user swipes horizontally in a row, the table view moves the row content aside to reveal your actions. Tapping one of the action buttons executes the handler block stored with the action object.

If you do not implement this method, the table view displays the standard accessory buttons when the user swipes the row.

因此您还需要将删除操作按钮与其他按钮一起传递。

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    let btnEdit = UITableViewRowAction(style: .default, title: "Edit") { action, indexPath in
         //Put the code of edit action
    }
    let btnDelete = UITableViewRowAction(style: .destructive, title: "Delete") { action, indexPath in
         //Put the code of delete action
    }
    return [btnEdit, btnDelete]
}

注意:如果您正在实现tableView(_:editActionsForRowAt:),则无需实现tableView(_:commit:forRowAt:) 方法。

关于ios - 向使用 Fetch Controller 的 TableView 添加编辑行操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41463859/

相关文章:

ios - 在 Xcode 中查看类成员文档的直接键盘快捷键?

ios - swift:在标签中显示秒数。

ios - TableView 索引被剪裁在底部

ios - 使用 UIApplicationDidEnterForeground 快速更新 tableview

ios - 如何在 swift 3 中从一个 View Controller 传递到前一个 View Controller 时设置委托(delegate)?

ios - Flurry 分析 - NSAutoreleasePool allocWithZone :] - memory leak after upgrading to Objective-C ARC

ios - UILabel 不会在 UIView 中居中

swift - 在 Swift 中获取 double 的小数部分

ios - 绘制 UITableView 的单元格会出现视觉错误

objective-c - Objective-C 如何调用 Swift 方法?