我已经更新了这个问题,因为我发现了这个问题的另一个关键。似乎当我向CoreData添加内容时,即使我可以打印出CoreData已保存的对象,tableview也不会重新加载数据。所以我知道数据是正确的,但功能
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
永远不会被召唤。
值得注意的是,如果我停止应用程序并重新打开它,表视图会显示正确数量的单元格和信息。所以似乎数据只在初始构建时加载属性。
我有一个UITableView
,CoreData
填充FetchedResultsController
。我添加了FRC委托方法,我试图强制tableView.reloadData()
方法,但这似乎不起作用。如果我停止构建并重建项目,数据会显示出来。
以下是我使用的委托方法:
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()
}
当我回到此视图时,我想强制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
}
答案 0 :(得分:0)
也许试试这个:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.tableView.reloadData()
}
答案 1 :(得分:0)
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
fetchedResultsController.delegate = nil
self.tableView.reloadData()
}
&#13;