我正在使用带有2个标签的TabBar控制器-Bills Reminder,Bills Paid。

在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

    }
}

每次我单击“已付账单”选项卡时,都会显示消息“嘿,我正在加载已付账单”,这部分没有问题。但是,尽管我正在使用.reloadData(),但该表未加载。

我在哪里错了?

最佳答案

单击“付费”标签时,您应该重新获取数据。为此,您只需在调用表_fetchedResultsController = nil之前在viewWillAppear函数中添加reloadData即可。

关于ios - 表格未在另一个TabBar ViewController中重新加载,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29764046/

10-11 23:47