我使用带有2个标签的TabBar控制器 - Bills Reminder,Bills Paid。
在Bills Reminder中设置记录的状态后,我进入了Bills Paid选项卡,它没有显示已标记为已付款的账单。
我退出应用并重新启动它,然后进入Bills Paid标签,它显示现在已经正确支付的所有账单。
我在Bills Paid View控制器中使用viewWillAppear。
BillsPaidTableViewController.swift
class BillsPaidTableViewController: UITableViewController, NSFetchedResultsControllerDelegate {
var fetchResultController:NSFetchedResultsController!
override func viewWillAppear(animated: Bool) {
println("Hey I'm loading bills paid")
self.tableView.reloadData()
super.viewWillAppear(false);
self.tableView.tableFooterView = UIView(frame: CGRectZero)
}
/*
comments: viewDidLoad will only load once and will not load after subsequent tapping on the the tab
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
self.tableView.tableFooterView = UIView(frame: CGRectZero)
}*/
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return self.fetchedResultsController.sections?.count ?? 0
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
let sectionInfo = self.fetchedResultsController.sections![section] as! NSFetchedResultsSectionInfo
return sectionInfo.numberOfObjects
}
var fetchedResultsController: NSFetchedResultsController {
if _fetchedResultsController != nil {
return _fetchedResultsController!
}
let fetchRequest = NSFetchRequest()
// Edit the entity name as appropriate.
if let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext {
let entity = NSEntityDescription.entityForName("Bills", inManagedObjectContext: managedObjectContext)
fetchRequest.entity = entity
// Set the batch size to a suitable number.
fetchRequest.fetchBatchSize = 20
// Edit the sort key as appropriate.
let predicate = NSPredicate(format: " paid == 1 ")
fetchRequest.predicate = predicate
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)
aFetchedResultsController.delegate = self
_fetchedResultsController = aFetchedResultsController
var error: NSError? = nil
if !_fetchedResultsController!.performFetch(&error) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
//println("Unresolved error \(error), \(error.userInfo)")
abort()
}
}
return _fetchedResultsController!
}
var _fetchedResultsController: NSFetchedResultsController? = nil
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("BillPaidCell", forIndexPath: indexPath) as! BillPaidTableViewCell
// Configure the cell...
self.configureCell(cell, atIndexPath: indexPath)
return cell
}
func configureCell(cell: BillPaidCell, atIndexPath indexPath: NSIndexPath) {
let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as! NSManagedObject
cell.billName?.text = object.valueForKey("name")!.description
cell.billAmt?.text = convertToDecimal( (object.valueForKey("amount")!.description as NSString).doubleValue , 2, 2)
cell.dateDue?.text = object.valueForKey("date_due")!.description
cell.datePaid?.text = object.valueForKey("date_paid")!.description
}
}
每次,我点击Bills Paid标签,它都会显示消息"嘿我正在加载已支付的账单",这部分没问题。但是,虽然我使用.reloadData(),但表格并未加载。
这部分我哪里出错?
答案 0 :(得分:1)
点击付费标签后,您应该重新获取数据。要执行此操作,您只需在_fetchedResultsController = nil
函数中添加viewWillAppear
,然后再调用表reloadData
。