ios - 表未在另一个 TabBar ViewController 中重新加载

标签 ios uitableview swift reloaddata

我正在使用带有 2 个选项卡的 TabBar Controller - 账单提醒、账单支付。

在Bills Reminder中设置记录状态后,我进入Bills Paid选项卡,它没有显示已标记为已支付的账单。

我退出应用程序并重新启动它,然后进入“已支付账单”选项卡,它现在显示所有已正确支付的账单。

我在我的 Bills Paid View Controller 中使用 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(),但表格并未加载。

这部分哪里出了问题?

最佳答案

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

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

相关文章:

ios - 将 NSCFString 转换为 NSString

ios - UISwipeGesture 'unrecognized selector sent to instance'

ios - 加载 UITableView 后停止事件指示器

ios - 用于更改标签背景颜色的表格单元格按钮操作

ios - 添加更多项目时滑动手势不起作用

ios - 在 WKWebView 中删除网站顶部导航栏

ios - 如何预加载 UICollectionVewCell?

ios - 如何从不同的类访问数组并向其添加更多对象?

iphone - iOS 6.0 Storyboard : Set static cell selected

ios - Swift 为 API REST 创建 Json