向使用 Fetch Controller 的 TableView 添加编辑行操作?
Adding Edit Row Action to TableView that uses Fetch Controller?
我有一个 table,它使用核心数据提取来填充其自身,提取控制器存储删除行操作。
但是我也想向该行添加一个编辑功能,我似乎无法将它与我目前拥有的删除功能一起添加,并且需要使用 editActionsForRowAt 来让它出现。
当我使用 editActionsForRowAt 时,它会覆盖现有的 Fetch Controllers 行操作并导致删除不再有效。
知道如何在现有删除旁边添加一个编辑,而不会过度写入或弄乱获取结果控制器吗?
这是获取控制器的代码,它是 table 及其删除案例和删除功能的来源,我想理想地向其添加一个编辑按钮,而不必全部覆盖它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)")
}
}
}
有关详细信息,请查看以下当前 Table 行编辑操作的屏幕截图。
来自文档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:)
方法。
我有一个 table,它使用核心数据提取来填充其自身,提取控制器存储删除行操作。
但是我也想向该行添加一个编辑功能,我似乎无法将它与我目前拥有的删除功能一起添加,并且需要使用 editActionsForRowAt 来让它出现。
当我使用 editActionsForRowAt 时,它会覆盖现有的 Fetch Controllers 行操作并导致删除不再有效。
知道如何在现有删除旁边添加一个编辑,而不会过度写入或弄乱获取结果控制器吗?
这是获取控制器的代码,它是 table 及其删除案例和删除功能的来源,我想理想地向其添加一个编辑按钮,而不必全部覆盖它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)")
}
}
}
有关详细信息,请查看以下当前 Table 行编辑操作的屏幕截图。
来自文档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:)
方法。