Why is the array not showing up in the table view?

╄→гoц情女王★ 提交于 2019-12-13 07:05:03

问题


I am appending data from parse into an array, but when I try to load array in table view nothing shows up. The array is populated, but nothing is showing up. How do I fix this?

class View2: UIViewController, UITableViewDataSource, UITableViewDelegate{

  var ret = [String]()

  func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return ret.count
  }

  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = table.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) 

    cell.textLabel?.text = ret[indexPath.row]

    return cell
   }
}

回答1:


After set 'ret' value, you have to reload table.

In my case,

var ret = [String](){
    didSet{
        self.tableView.reloadData()
    }
}



回答2:


in my case I forgot to connect the UITableView datasource outlet to the ViewController.




回答3:


Ensure your tableview is being reloaded after you receive the data. Use tableViewName.reloadData()




回答4:


Add a call to reloadData() method of UITableView in the setter of your array.



来源:https://stackoverflow.com/questions/33537391/why-is-the-array-not-showing-up-in-the-table-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!