2015-10-19 40 views
0

我想用服務器數據更新tableview。UITableView numberOfRows

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 

    @IBOutlet weak var tableView: UITableView! 
    var aedList = [AED]() 


    override func viewDidLoad() { 
     super.viewDidLoad() 
    } 

    override func viewWillAppear(animated: Bool) { 
    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return self.aedList.count 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cellIdentifier = "Cell" 
     let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) 

     Server().getJsonInformationFromServer(url: "aeds") { (response) -> Void in 

      self.aedList = JSONHelper().parseAEDInformation(json: response["data"]) 

      dispatch_async(dispatch_get_main_queue(), {() -> Void in 
       cell.textLabel?.text = self.aedList[indexPath.row].street 
       self.tableView.reloadData() 
      }) 
     } 

     return cell 
    } 

正在正確下載數據,但tableview爲空。 我想這是由於我的下載異步行爲,並且numberOfRows方法試圖計數仍然是空的數組。

如何更正此問題才能正確顯示數據?我是否也需要使用dispatch_async?

回答

2

的cellForRowAtIndexPath作爲方法顧名思義,它通過爲每個單元格的表格視圖調用,所以你不應該獲取整個數據存在,其分別填充細胞。以下是代碼中的一些修正:

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 

    @IBOutlet weak var tableView: UITableView! 
    var aedList = [AED]() 

    //In addition to viewDidLoad, you can call this method whenever you wanna update your table 
    func fetchData() { 
     dispatch_async(dispatch_get_main_queue()) { 
      Server().getJsonInformationFromServer(url: "aeds") { (response) -> Void in 
       self.aedList = JSONHelper().parseAEDInformation(json: response["data"]) 
       self.tableView.reloadData() 
      }) 
     } 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     fetchData() 
    } 
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 1 //you can ofc change this 
    } 
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return self.aedList.count 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cellIdentifier = "Cell" 
     let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) 
     cell.textLabel?.text = self.aedList[indexPath.row].street 
     return cell 
    } 
+0

謝謝盧卡斯!我完全忘記了我正在從我的方法中爲每個單元格的服務器加載內容的事實! – sesc360

+0

它發生:),我很高興我幫助 – Lukas

+0

'numberOfSectionInTableView'是可選的,你不需要實現它。它默認返回1. – Sulthan

1

tableView:cellForRowAtIndexPath:加載數據不會無效。這種方法被稱爲每一個細胞,這意味着:

  1. 如果沒有細胞(數據尚未加載),該方法不會永遠不會被調用,您的數據就不會被加載(這是現在發生了什麼)。

  2. 如果數據已加載,您將爲每個顯示的單元觸發一次新的加載,也就是說,如果您有5個項目,則會觸發5次加載。這是遞歸的,因爲reloadData將重新顯示所有單元格。

解決方案:

把你的代碼加載到一個更好的地方,例如viewDidAppear您的控制器的方法。

override func viewDidAppear(animated: Bool) { 
    super.viewDidAppear(animated) 

    Server().getJsonInformationFromServer(url: "aeds") { (response) -> Void in 

     self.aedList = JSONHelper().parseAEDInformation(json: response["data"]) 
     dispatch_async(dispatch_get_main_queue(), {() -> Void in 
      self.tableView.reloadData() 
     }) 
    } 

} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cellIdentifier = "Cell" 
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) 

    cell.textLabel?.text = self.aedList[indexPath.row].street 

    return cell 
} 
+0

謝謝蘇爾坦,非常有幫助! – sesc360