gpt4 book ai didi

ios - 如何使用 NSFetchedResultsController 更新 tableview 内容

转载 作者:行者123 更新时间:2023-12-03 21:25:04 25 4
gpt4 key购买 nike

我正在更新 coredata 的某些部分,并且我希望 NSFetchedResultsController 自动重新加载包含所有动画的表格 View 。我正在更改用于对 TableView 进行排序的 coredata 元素。我通过在单元格上向右滑动,然后将该特定 tableviewcell 的priorityNumber更改为零来完成此操作,以便任务转到底部和其他一些事情。我希望表格重新加载但带有动画。现在我只是在做 tableView.reload() 但它不能正常工作。

我想看到单元格移动到设备底部的动画。任何帮助将不胜感激!

代码如下:我对 coreData 所做的更改:

func tableView(_ tableView: UITableView,
leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
{
let closeAction = UIContextualAction(style: .normal, title: "✓", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in

//???

self.fetchedResultsController.object(at: indexPath).sTaskCompleted = !(self.fetchedResultsController.object(at: indexPath).sTaskCompleted)

if ( self.fetchedResultsController.object(at: indexPath).sTaskCompleted)
{
print("Completed")
//Change priority number to 0 after saving their current priority number in previousPN
//Change priority circle color
//Change background of the cell to something else




self.fetchedResultsController.object(at: indexPath).sPreviousPN = self.fetchedResultsController.object(at: indexPath).sPriorityNumber
self.fetchedResultsController.object(at: indexPath).sPriorityNumber = 0
self.fetchedResultsController.object(at: indexPath).cellbackgroundColor = UIColor(red:0.93, green:0.91, blue:0.91, alpha:1.0)
self.fetchedResultsController.object(at: indexPath).previousPriorityColor = self.fetchedResultsController.object(at: indexPath).sPriorityColor
self.fetchedResultsController.object(at: indexPath).sPriorityColor = .clear

var error: NSError?


do {
// Save The object

try self.moContext.save()
print("SAVED")
} catch let error1 as NSError {
error = error1
}


DispatchQueue.main.asyncAfter(deadline: .now() + 0.3, execute: {
UIView.transition(with: tableView,
duration: 0.15,
options: .transitionCrossDissolve,
animations: { self.tableView.reloadData() })
})

} else {
print("InCompleted")


self.fetchedResultsController.object(at: indexPath).sPriorityNumber = self.fetchedResultsController.object(at: indexPath).sPreviousPN

self.fetchedResultsController.object(at: indexPath).cellbackgroundColor = .white
self.fetchedResultsController.object(at: indexPath).sPriorityColor = self.fetchedResultsController.object(at: indexPath).previousPriorityColor

var error: NSError?


do {
// Save The object

try self.moContext.save()
print("SAVED")
} catch let error1 as NSError {
error = error1
}
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3, execute: {
UIView.transition(with: tableView,
duration: 0.15,
options: .transitionCrossDissolve,
animations: { self.tableView.reloadData() })
})

}

success(true)

})

closeAction.backgroundColor = UIColor(red:0.18, green:0.87, blue:0.65, alpha:1.0)

return UISwipeActionsConfiguration(actions: [closeAction])

}

现在如何使用 FRC 委托(delegate)方法 (.update) 用适当的动画刷新 tableView。

最佳答案

TableView Controller 需要符合 NSFetchedResultsControllerDelegate 并实现其委托(delegate)方法。如果配置正确,获取的结果 Controller 将监视托管对象上下文的更改,并调用适当的委托(delegate)方法以使用动画更新 TableView 。

class TaskTableViewController: UITableViewController, NSFetchedResultsControllerDelegate {

var fetchedResultsController: NSFetchedResultsController<Task>!

override func viewDidLoad() {
super.viewDidLoad()
configureFetchedResultsController()
}

func configureFetchedResultsController() {

if fetchedResultsController == nil {

// 1. Get fetch request
let fetchRequest: NSFetchRequest<Task> = Task.fetchRequest()

// 2. Add sort descriptors
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "sPriorityNumber", ascending: false)]

// 3. Configure the FRC
fetchedResultsController = NSFetchedResultsController<Task>(fetchRequest: fetchRequest, managedObjectContext: CoreDataStack.context, sectionNameKeyPath: nil, cacheName: nil)

// 4. Set the FRC delegate as self
fetchedResultsController.delegate = self
}

// 5. Tell the FRC to start watching the MOC for changes
do {
try fetchedResultsController.performFetch()
} catch {
NSLog("Error starting fetched results controller \(error)")
}
}

// FRC delegate methods

func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>,
didChange anObject: Any,
at indexPath: IndexPath?,
for type: NSFetchedResultsChangeType,
newIndexPath: IndexPath?) {
switch type {
case .insert:
guard let newIndexPath = newIndexPath else { return }
tableView.insertRows(at: [newIndexPath], with: .automatic)
case .delete:
guard let indexPath = indexPath else { return }
tableView.deleteRows(at: [indexPath], with: .fade)
case .move:
tableView.reloadData()
case .update:
guard let indexPath = indexPath else { return }
tableView.reloadRows(at: [indexPath], with: .automatic)
}
}

func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>,
didChange sectionInfo: NSFetchedResultsSectionInfo,
atSectionIndex sectionIndex: Int,
for type: NSFetchedResultsChangeType) {
switch type {
case .delete:
tableView.deleteSections(IndexSet(integer: sectionIndex), with: .automatic)
case .insert:
tableView.insertSections(IndexSet(integer: sectionIndex), with: .automatic)
default:
break
}
}
}

关于ios - 如何使用 NSFetchedResultsController 更新 tableview 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48254060/

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