为什么数组没有显示在 table 视图中?

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

我正在将解析中的数据附加到一个数组中,但是当我尝试在 table 视图中加载数组时,没有任何显示。该阵列已填充,但没有显示任何内容。我该如何解决这个问题?

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
   }
}

确保在收到数据后重新加载 tableview。使用 tableViewName.reloadData()

在数组的 setter 中添加对 UITableViewreloadData() 方法的调用。

设置 'ret' 值后,您必须重新加载 table。

就我而言,

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

在我的例子中,我忘记将 UITableView 数据源出口连接到 ViewController。