gpt4 book ai didi

ios - swift ,核心数据 : TableView not reloading data when childVC dismissed

转载 作者:行者123 更新时间:2023-11-28 17:50:22 27 4
gpt4 key购买 nike

好吧,我对此是全新的,并且在阅读许多教程和文章时有点不知所措。并花了几个小时来解决类似的问题,但没有运气解决我自己的问题。我有一个“AddSiteVC”,允许用户添加或删除放入 CoreData 的项目,然后显示在我的“MainVC”上的 TableView 中。我的问题是,当我按保存或删除并返回到我的 MainVC onBtnClick 时,TableView 不会更新,直到我离开 MainVC 然后回来。我不知道我做错了什么,但似乎找不到任何解决这个问题的方法……我不知道我的问题出在哪里,所以我将包括我的大部分 MainVC 代码以供引用。任何帮助将不胜感激!谢谢!

import UIKit
import CoreData

class SitesViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, NSFetchedResultsControllerDelegate {

@IBOutlet weak var tableView: UITableView!

var controller: NSFetchedResultsController<Sites>!

override func viewDidLoad() {
super.viewDidLoad()

tableView.delegate = self
tableView.dataSource = self

attemptFetch()
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SitesCell", for: indexPath) as! SitesCell
configureCell(cell: cell, indexPath: indexPath as NSIndexPath)
return UITableViewCell()
}

func configureCell(cell: SitesCell, indexPath: NSIndexPath) {
let sites = controller.object(at: indexPath as IndexPath)
cell.configureCell(sites: sites)
cell.accessoryType = .detailButton
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "AddSiteViewController" {
if let destination = segue.destination as? AddSiteViewController {
if let site = sender as? Sites {
destination.siteToEdit = site
}
}
}
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let sections = controller.sections {
let sectionInfo = sections[section]
return sectionInfo.numberOfObjects
}
return 0
}

func numberOfSections(in tableView: UITableView) -> Int {
if let sections = controller.sections {
return sections.count
}
return 0
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 75
}

func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
if let objs = controller.fetchedObjects, objs.count > 0 {
let site = objs[indexPath.row]
performSegue(withIdentifier: "AddSiteViewController", sender: site)
}
}

func attemptFetch() {
let fetchRequest: NSFetchRequest<Sites> = Sites.fetchRequest()
let alphebaticalSort = NSSortDescriptor(key: "name", ascending: true)
fetchRequest.sortDescriptors = [alphebaticalSort]

let controller = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
controller.delegate = self
self.controller = controller
do {
try controller.performFetch()
} catch {
let error = error as NSError
print("\(error)")
}
}

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

func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
switch (type) {
case.insert:
if let indexPath = newIndexPath {
tableView.insertRows(at: [indexPath], with: .fade)
}
break
case.delete:
if let indexPath = indexPath {
tableView.deleteRows(at: [indexPath], with: .fade)
}
break
case.update:
if let indexPath = indexPath {
let cell = tableView.cellForRow(at: indexPath) as! SitesCell
configureCell(cell: cell, indexPath: indexPath as NSIndexPath)
}
break
case.move:
if let indexPath = indexPath {
tableView.deleteRows(at: [indexPath], with: .fade)
}
if let indexPath = newIndexPath {
tableView.insertRows(at: [indexPath], with: .fade)
}
break
}
}

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

最佳答案

请对代码进行以下更改。因为 viewDidLoad 会在 viewcontroller 加载的时候被调用。但根据您的要求,您在模态页面中添加了一些内容。所以你需要将代码移动到 viewWillAppear

import UIKit
import CoreData

class SitesViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, NSFetchedResultsControllerDelegate {

@IBOutlet weak var tableView: UITableView!

var controller: NSFetchedResultsController<Sites>!

override func viewDidLoad() {
super.viewDidLoad()

tableView.delegate = self
tableView.dataSource = self


}

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
attemptFetch()
}
}

关于ios - swift ,核心数据 : TableView not reloading data when childVC dismissed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40253849/

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