Table 没有在另一个 TabBar 中重新加载 ViewController
Table not reloading in another TabBar ViewController
我正在使用带有 2 个选项卡的 TabBar 控制器 - 账单提醒、账单支付。
在 Bills Reminder 中设置记录状态后,我进入 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
}
}
每次我点击支付账单选项卡,它都会显示消息"Hey I'm loading bills paid",这部分没问题。但是,尽管我正在使用 .reloadData(),table 并未加载。
我这部分哪里出错了?
您应该在点击付费标签时重新获取数据。为此,您只需在调用 table reloadData
.
之前在 viewWillAppear
函数中添加 _fetchedResultsController = nil
我正在使用带有 2 个选项卡的 TabBar 控制器 - 账单提醒、账单支付。
在 Bills Reminder 中设置记录状态后,我进入 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
}
}
每次我点击支付账单选项卡,它都会显示消息"Hey I'm loading bills paid",这部分没问题。但是,尽管我正在使用 .reloadData(),table 并未加载。
我这部分哪里出错了?
您应该在点击付费标签时重新获取数据。为此,您只需在调用 table reloadData
.
viewWillAppear
函数中添加 _fetchedResultsController = nil