gpt4 book ai didi

swift - 更改数据和 popViewController 后 TableView 不会重新加载

转载 作者:行者123 更新时间:2023-11-30 10:36:39 25 4
gpt4 key购买 nike

在我的应用程序中,我使用 NSFetchedResultsControllerDelegate 在添加新数据后重新加载 Tableview,但此解决方案仅在我第一次访问 View 时才有效。

如果我删除数据并再次添加,该信息不会出现在表格 View 中。为此,我需要离开 View 并再次访问它。

我尝试过使用 Delegates 和 CallBack 来重新加载 TableView ,但没有成功。

我发现在删除旧数据后添加新数据后不会调用 cellForRowAt 。尽管已经调用了 numberOfSections。

我迷路了。有人可以帮助我吗?

Here is the video with app behaviour

在这里我向您展示我的代码的一些部分:

我的 NSFetchedResultsControllerDelegate

extension MeasureController: NSFetchedResultsControllerDelegate {

func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange sectionInfo: NSFetchedResultsSectionInfo, atSectionIndex sectionIndex: Int, for type: NSFetchedResultsChangeType) {
switch type {
case .insert:
myTableView.insertSections(IndexSet(integer: sectionIndex), with: .fade)
case .delete:
myTableView.deleteSections(IndexSet(integer: sectionIndex), with: .fade)
case .move:
break
case .update:
myTableView.reloadSections(IndexSet(integer: sectionIndex), with: .fade)
@unknown default:
fatalError()
}
}


func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
myTableView.reloadData()
myCollectionView.reloadData()
}

//Allow to show full section name in header.
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, sectionIndexTitleForSectionName sectionName: String) -> String? {
return sectionName
}

}

我的 MeasureController 中具有表格 View 的 viewWillAppear

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

self.fillDataInTheView()
self.allMeasures = CoreDataManager.shared.loadAllMeasuresByAthlete(athlete: athleteToShow)

//Get the last recorded date that has measures records
if let allMeasures = self.allMeasures, let selectedDay = CoreDataManager.shared.getLastDayWithMeasures(measure: allMeasures) {
self.selectedDay = selectedDay
}

if selectedDay != nil {
if let measureByAthleteAndDay = CoreDataManager.shared.loadAllMeasuresByAthleteAndDay(athlete: athleteToShow, day: selectedDay) {
self.measureByAthleteAndDay = measureByAthleteAndDay
}

do {
try fetchedRCAllMeasuresByDay.performFetch()
} catch let fetchedError {
print("Error fetching Measures by Day:", fetchedError)
}
}

do {
try fetchedRCDay.performFetch()
} catch let err {
print(err)
}

if let lastEvaluation = getRecentDate() {
lbEvaluationDate.text = "Última Avaliação: \(lastEvaluation.mediumDate())"
} else {
lbEvaluationDate.text = "Sem Avaliação Registrada"
}

self.myTableView.reloadData()
}

这是我的日期数据 myCollectioView

extension MeasureController: UICollectionViewDelegate, UICollectionViewDataSource {

// MARK: - Collection View Data Source

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
guard let dates = fetchedRCDay.fetchedObjects else {return 0}
return dates.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = myCollectionView.dequeueReusableCell(withReuseIdentifier: cellCollectionViewID, for: indexPath) as! DateCollectionCell
let day = fetchedRCDay.object(at: indexPath)
if let date = day.title {
cell.setupCell(date: date)
}

if day == selectedDay {
cell.isSelected = true
} else {
cell.isSelected = false
}

cell.delegate = self

return cell
}

// MARK: - Collection View Delegate

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
self.selectedCell(true, indexPath: indexPath)
let day = fetchedRCDay.object(at: indexPath)
self.alertToCreateEvaluation(day: day)
myCollectionView.reloadData()
}

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
self.selectedCell(false, indexPath: indexPath)
let day = fetchedRCDay.object(at: indexPath)
if let date = day.title {
lbEvaluationDate.text = "Data da Avaliação: \(date.mediumDate())"
}
}

func hasMeasures(cell: DateCollectionCell, day: Day) {

if day.allMeasures == nil {
cell.ivIndicator.isHidden = true
} else {
cell.ivIndicator.isHidden = false
}
}

}

如果您需要更多代码来更好地理解它,请告诉我。

最佳答案

试试这个...根据 Apple 文档 NSFetchedResultControllerDelegate ...

添加新的实例方法:

func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {

tableView.beginUpdates()
}

更改现有的 controllerDidChangeContent(_:) 实例方法:

func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {

tableView.endUpdates()
}

省略对 .reloadData() 的调用 - 这些是“强力”便捷方法,可以重新加载整个表或 Collection View ,并使微妙的 FRC 委托(delegate)方法的使用变得多余。

关于swift - 更改数据和 popViewController 后 TableView 不会重新加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57843221/

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