gpt4 book ai didi

ios - 使用 FRC Swift 3 更改 CoreData 后,TableView 不会更新(reloadData)

转载 作者:行者123 更新时间:2023-11-29 00:32:56 28 4
gpt4 key购买 nike

我已经更新了这个问题,因为我发现了这个问题的另一个关键。似乎当我向 CoreData 添加内容时,tableview 不会重新加载数据,即使我可以打印出 CoreData 保存的对象。所以我知道数据是正确的,但是函数func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 永远不会被调用。

还值得注意的是,如果我停止应用程序并重新打开它,表格 View 会显示正确数量的单元格和信息。所以看起来数据只在初始构建时加载属性。

我有一个 UITableView,它由 CoreData 填充并且还使用了一个 FetchedResultsController。我添加了 FRC 委托(delegate)方法并尝试强制使用 tableView.reloadData() 方法,但这似乎不起作用。如果我停止构建并重新构建项目,数据就会显示出来。

这是我正在使用的委托(delegate)方法:

    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:
tableView.insertRows(at: [newIndexPath!], with: .automatic)
case .delete:
tableView.deleteRows(at: [indexPath!], with: .automatic)
case .update:
tableview.reloadData()
case .move:
tableView.deleteRows(at: [indexPath!], with: .automatic)
tableView.insertRows(at: [newIndexPath!], with: .automatic)
}
}

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

当我回到这个 View 时,我想强制 TableView 重新加载它的数据。

ViewwillAppear 方法:

override func viewWillAppear(_ animated: Bool) {
// load the data

let fetchRequest: NSFetchRequest<Person> = Person.fetchRequest()
fetchRequest.predicate = NSPredicate(format:"statement.@sum.amountOwed >= 0")
let sort = NSSortDescriptor(key: #keyPath(Person.name), ascending: true)
fetchRequest.sortDescriptors = [sort]
positiveFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: coreDataStack.managedContext, sectionNameKeyPath: nil, cacheName: nil)

do{
try positiveFetchedResultsController.performFetch()
}catch let error as NSError{
print("Fetching error: \(error), \(error.userInfo)")
}


let negativFetchRequest: NSFetchRequest<Person> = Person.fetchRequest()
negativFetchRequest.predicate = NSPredicate(format:"statement.@sum.amountOwed < 0")
let negativeSort = NSSortDescriptor(key: #keyPath(Person.name), ascending: true)
negativFetchRequest.sortDescriptors = [negativeSort]
negativeFetchedResultsController = NSFetchedResultsController(fetchRequest: negativFetchRequest, managedObjectContext: coreDataStack.managedContext, sectionNameKeyPath: nil, cacheName: nil)

do{
try negativeFetchedResultsController.performFetch()
}catch let error as NSError{
print("Fetching error: \(error), \(error.userInfo)")
}

positiveFetchedResultsController.delegate = self
negativeFetchedResultsController.delegate = self

print("\(positiveFetchedResultsController.fetchedObjects!.count) positive fetch count")
//print("\(positiveFetchedResultsController.fetchedObjects![0].statement!.count) positive statements count")
print("\(negativeFetchedResultsController.fetchedObjects!.count) negative fetch count")
//print("\(negativeFetchedResultsController.fetchedObjects![0].statement!.count) negative statements count")

tableView.reloadData()
}

这是我的 cellForRowAt 方法:

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "personCell", for: indexPath) as! PersonTableViewCell

switch(indexPath.section) {
case 0:
//print("this is section 0")
let person = positiveFetchedResultsController.object(at: indexPath)
cell.personName.text = person.name
//print("\(person.name!) is the name")
//print("\(person.statement!.count) postive person statement count")

if(person.statement!.count == 0){
print("default")
cell.statementAmount.text = "$0.00"
}
else{
//print("\(person.name!) has \(person.statement!.count) statement count")
let amountTotal = person.value(forKeyPath: "statement.@sum.amountOwed") as? Decimal
//print("\(amountTotal!) this is the total")
cell.statementAmount.text = convertStringToDollarString(amountToConvert: String(describing: amountTotal!))

}

case 1:

print("this is section 1")
//print("\(negativeFetchedResultsController.object(at: [indexPath.row,0])) objects fetched")
//print("\(indexPath.section) section number")
//print("\(indexPath.row) row number")

let person = negativeFetchedResultsController.fetchedObjects![indexPath.row]
cell.personName.text = person.name

print("\(person.name!) is the name")
print("\(person.statement!.count) negative person statement count")

if(person.statement!.count == 0){
cell.statementAmount.text = "$0.00"
}
else{
//print("\(person.name!) has \(person.statement!.count) statement count")
let amountTotal = person.value(forKeyPath: "statement.@sum.amountOwed") as? Decimal
print("\(amountTotal!) this is the total")
cell.statementAmount.text = String(describing: amountTotal!)

cell.statementAmount.text = convertStringToDollarString(amountToConvert: String(describing: amountTotal!))
}

cell.backgroundColor = Styles.redColor();

let bgColorView = UIView()
bgColorView.backgroundColor = Styles.darkRedColor()
cell.selectedBackgroundView = bgColorView

default: cell.personName.text = "hello"
}
return cell
}

最佳答案

也许试试这个:

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.tableView.reloadData()
}

关于ios - 使用 FRC Swift 3 更改 CoreData 后,TableView 不会更新(reloadData),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41296550/

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