gpt4 book ai didi

ios - 使用核心数据的具有多个部分的 Diffable 数据源

转载 作者:行者123 更新时间:2023-12-02 02:43:40 29 4
gpt4 key购买 nike

WWDC 2019 确实充满了 iOS 的新内容以及 TableViewsCollectionView 的新数据源,即 UITableViewDiffableDataSource .

我已成功将上述新数据源与核心数据集成,删除和插入新记录工作没有任何问题,不幸的是,我在将项目从一个部分移动到另一个部分时遇到问题,如果我尝试移动该部分的最后一个单元格。

下面是我的代码:

表格 View 设置

private func setupTableView() {
diffableDataSource = UITableViewDiffableDataSource<Int, Reminder>(tableView: remindersTableView) { (tableView, indexPath, reminder) -> UITableViewCell? in
let cell = tableView.dequeueReusableCell(withIdentifier: "SYReminderCompactCell", for: indexPath) as! SYReminderCompactCell
var reminderDateString = ""
let reminderTitle = "\(reminder.emoji ?? "") \(reminder.title ?? "")"

if let date = reminder.date {// check if reminder has date or no , if yes check number of todos and if date in today
let dateFormatter = SYDateFormatterManager.sharedManager.getDateFormaatter()
dateFormatter.dateStyle = .none
dateFormatter.timeStyle = .short
reminderDateString = dateFormatter.string(from: date)
}

let toDosList = SYCoreDataManager.sharedManager.fetchAllToDosToOneReminder(reminder: reminder)
cell.indexPath = indexPath
cell.showMoreDelegate = self
cell.initializeToDosCompactView(toDoList: toDosList ?? [],reminderTitleText: reminderTitle,reminderDateText: reminderDateString)
cell.changeTextViewStyle(isChecked: reminder.isCompleted)

return cell
}

setupSnapshot(animated: true)
}

使用 TableView 数据创建一个NSDiffableDataSourceSnapshot

private func setupSnapshot(animated: Bool) {
diffableDataSourceSnapshot = NSDiffableDataSourceSnapshot<Int, Reminder>()

for (i , section) in (fetchedResultsController.sections?.enumerated())! {
diffableDataSourceSnapshot.appendSections([i])
let items = section.objects
diffableDataSourceSnapshot.appendItems(items as! [Reminder])
diffableDataSource?.apply(self.diffableDataSourceSnapshot, animatingDifferences: animated, completion: nil)
}

}

NSFetchedResultsControllerDelegate 用于部分和行

     func controller(_ controller: 
NSFetchedResultsController<NSFetchRequestResult>, didChange
anObject: Any, at indexPath: IndexPath?, for type:
NSFetchedResultsChangeType,
newIndexPath: IndexPath?) {

switch type {
case .insert:
if let indexPath = newIndexPath {
let section = fetchedResultsController.sections![indexPath.section]
self.diffableDataSourceSnapshot.appendItems(section.objects as! [Reminder], toSection: indexPath.section)
self.diffableDataSource?.apply(self.diffableDataSourceSnapshot, animatingDifferences: true)

}
break
case .update:
break
case .delete:

if let indexPath = indexPath {
guard let item = self.diffableDataSource?.itemIdentifier(for: indexPath) else { return }
self.diffableDataSourceSnapshot.deleteItems([item])
self.diffableDataSource?.apply(self.diffableDataSourceSnapshot, animatingDifferences: true)
}
break
case .move:
if let indexPath = indexPath {
guard let item = self.diffableDataSource?.itemIdentifier(for: indexPath) else { return }
self.diffableDataSourceSnapshot.appendSections([indexPath.section])
self.diffableDataSourceSnapshot.deleteItems([item])
self.diffableDataSource?.apply(self.diffableDataSourceSnapshot, animatingDifferences: true)
}
if let newIndexPath = newIndexPath {
let section = fetchedResultsController.sections![newIndexPath.section]

// let items = fetchedResultsController.object(at: indexPath)
print("snapppp" , diffableDataSourceSnapshot.sectionIdentifiers)
let items = section.objects as! [Reminder]

self.diffableDataSourceSnapshot.appendItems(items, toSection: newIndexPath.section)
self.diffableDataSource?.apply(self.diffableDataSourceSnapshot, animatingDifferences: true)
}
break
}
}

func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange sectionInfo: NSFetchedResultsSectionInfo, atSectionIndex sectionIndex: Int, for type: NSFetchedResultsChangeType) {
switch type {
case .insert:
setupSnapshot(animated: false)
break
case .update:
break
case .delete:
let section = self.diffableDataSourceSnapshot.sectionIdentifiers[sectionIndex]
self.diffableDataSourceSnapshot.deleteSections([section])
self.diffableDataSource?.apply(self.diffableDataSourceSnapshot, animatingDifferences: true)
//setupSnapshot(animated: false)

break
case .move:
break
}
}

最佳答案

  • 为了顺利使用 Core Data,数据源必须声明为

    UITableViewDiffableDataSource<String,NSManagedObjectID>
  • setupTableView中将闭包参数标签重命名为

    (tableView, indexPath, objectID) -> UITableViewCell? in

    并通过以下方式获取提醒

    let reminder = self.fetchedResultsController.object(at: indexPath)

    let reminder = try! self.managedObjectContext.existingObject(with: objectID) as! Reminder
  • 然后替换整个方法

    func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, 
    didChange anObject: Any,
    at indexPath: IndexPath?,
    for type: NSFetchedResultsChangeType,
    newIndexPath: IndexPath?) { ... }

    func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, 
    didChange sectionInfo: NSFetchedResultsSectionInfo, atSectionIndex sectionIndex: Int,
    for type: NSFetchedResultsChangeType) { ... }

    只是与

    func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChangeContentWith snapshot: NSDiffableDataSourceSnapshotReference) {       
    self.diffableDataSource.apply(snapshot as NSDiffableDataSourceSnapshot<String, NSManagedObjectID>, animatingDifferences: true)
    }
  • 同时删除方法 setupSnapshot,它不再需要。在调用performFetch之后以及托管对象上下文发生任何变化时,框架会正确创建快照并调用委托(delegate)方法。

关于ios - 使用核心数据的具有多个部分的 Diffable 数据源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58510374/

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