2016-11-23 56 views
0

我非常喜欢Swift初学者 - 从Firebase数据填充表格视图。Swift - 当数据实际加载到TableViewController

在表格页脚中,我想在表格列下显示一些计算出的总计。但是,在致电footerCell.configure(priceLines, isPortrait: isPortrait)时,priceLines字典仍为空。

如何解决这个问题?

由于提前,安德烈·哈特曼,比利时

import UIKit 
import FirebaseDatabase 

class ListTableViewController: UITableViewController { 
    var priceLines = [NSDictionary]() 
    var isPortrait = false 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ListTableViewController.rotated), name: UIDeviceOrientationDidChangeNotification, object: nil) 
     loadDataFromFirebase() 
    } 
    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
    } 
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 1 
    } 
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return priceLines.count 
    } 
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("profileCell", forIndexPath: indexPath) as! PriceTableViewCell 
     cell.configure(priceLines, row: indexPath.row, isPortrait: isPortrait, source: "intraday") 
     return cell 
    } 
    override func tableView(tableView: UITableView,viewForHeaderInSection section: Int) -> UIView? { 
     let headerCell = tableView.dequeueReusableCellWithIdentifier("HeaderCell") as! CustomHeaderCell 
     headerCell.configure(isPortrait) 
     return headerCell 
    } 
    override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { 
     let footerCell = tableView.dequeueReusableCellWithIdentifier("FooterCell") as! CustomFooterCell 
     footerCell.configure(priceLines, isPortrait: isPortrait) 
     return footerCell 
    } 
    override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { 
     return 30.0 
    } 
    override func tableView (tableView:UITableView, heightForHeaderInSection section:Int) -> CGFloat 
    { 
     return 50.0; 
    } 

    // MARK:- Load data from Firebase 
    func loadDataFromFirebase() { 
     UIApplication.sharedApplication().networkActivityIndicatorVisible = true 
     refInter.observeEventType(.Value, withBlock: { snapshot in 
      var tempItems = [NSDictionary]() 
      for item in snapshot.children { 
       let child = item as! FIRDataSnapshot 
       let dict = child.value as! NSDictionary 
       tempItems.append(dict) 
      } 
      self.priceLines = tempItems 
      self.tableView.reloadData() 
      UIApplication.sharedApplication().networkActivityIndicatorVisible = false 
     }) 
    } 
    func rotated() 
    { 
     let newDisplay = (UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation)) 
     if(newDisplay != isPortrait){ 
      self.tableView.reloadData() 
     } 
     isPortrait = newDisplay 
    } 
} 
+0

当你调用'reloadData ()',但如果在第一次加载数据之前旋转屏幕,则不会发生任何事情,因为Firebase获取是异步工作的。 – vadian

回答

0

clearly says

当表视图即将出现它的加载,所述 表视图控制器重新加载所述第一时间的文档表格视图的数据。

因此,它会自动重新加载表格viewDidLoadviewWillAppear之间的某个位置。您的priceLines此时为空,只有在方法loadDataFromFirebase中的关闭被触发时才会填充数据。我不确定什么时候发生在你的情况下,但是当你隐含地呼叫reloadData那么你应该已经有priceLines非空(当然如果封闭结果有一些数据)

相关问题