2015-10-19 41 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