gpt4 book ai didi

ios - 表删除操作删除单元格内容而不是单元格本身?

转载 作者:搜寻专家 更新时间:2023-11-01 06:04:07 24 4
gpt4 key购买 nike

我的表有一个删除函数,它是获取 Controller 的委托(delegate)。当我滑动删除一行时,它会清除对象并删除其数据,但该单元格仍然是一个空行。

我的获取和表格代码看起来像这样,知道为什么它不删除该行吗?:

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

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {

let delete = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in
let UserExercise = self.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)")
}
self.workoutDesignerTable.reloadData()
}

let edit = UITableViewRowAction(style: .normal, title: "Edit") { (action, indexPath) in
let cell = tableView.cellForRow(at: indexPath)
self.updateExercise = self.fetchedResultsController.object(at: indexPath)
self.performSegue(withIdentifier: self.segueEditUserExerciseViewController, sender: cell)
}
edit.backgroundColor = UIColor.lightGray
return [delete, edit]
}

问题图片如下:

enter image description here

enter image description here

enter image description here

我添加了以下功能,但没有解决问题

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

这里还有请求的 numberOfRowsInSection

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
guard let userExercises = fetchedResultsController.fetchedObjects else { return 0 }
return userExercises.count
}

enter image description here

我已经添加了上面的控制台打印以在删除对象的打印后显示,如果这有助于缩小范围,它似乎被读取为有错误

在下面添加了 fetchresultscontroller 代码

fileprivate lazy var fetchedResultsController: NSFetchedResultsController<UserExercise> = {

let fetchRequest: NSFetchRequest<UserExercise> = UserExercise.fetchRequest()
if self.userRoutine == nil {
fetchRequest.predicate = NSPredicate(format: "usersroutine == nil")
} else if self.userRoutine != nil {
fetchRequest.predicate = NSPredicate(format: "usersroutine.name == %@", self.userRoutine!)
}
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "dateCreated", ascending: true)]

//controller config
let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.persistentContainer.viewContext, sectionNameKeyPath: nil, cacheName: nil)

fetchedResultsController.delegate = self
return fetchedResultsController
}()

附加 Controller 委托(delegate)代码:

func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
workoutDesignerTable.beginUpdates()
}

func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
workoutDesignerTable.endUpdates()
updateView()
}

// MARK: - ADDING TABLE ROW AND EDITORIAL FEATURES

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

最佳答案

您需要执行 3 个步骤才能实现您想要的效果。检测滑动、从模型中移除、更新 View 。这是一个例子:

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { // Detect Swipe
if editingStyle == .delete {
restaurantNames.remove(at: indexPath.row) // Remove from Model
}
tableView.deleteRows(at: [indexPath], with: .fade) // Update view
}

关于ios - 表删除操作删除单元格内容而不是单元格本身?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41875963/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com