如何在Swift中填充表格视图?

时间:2015-09-26 16:05:21

标签: ios swift uitableview

我写了这段代码:

//Basic methods
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    self.DevicesTableView.registerClass(UITableViewCell.self,forCellReuseIdentifier: "cell")
    self.DevicesTableView.dataSource = self

}


var array = ["one","two"]

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell: UITableViewCell! = self.DevicesTableView
        .dequeueReusableCellWithIdentifier("cell") as UITableViewCell!

    cell.textLabel!.text = self.array[indexPath.row]

    return cell
}

所以代码工作得很好,并向我展示了填充的tableview:

Row1“One” 第2行“两个”

但是如何在程序开头但不是在其他任何时候填写TableView?我试图在viewDidLoad的另一个函数中调用下面的方法,但它不起作用......为什么?

self.DevicesTableView.registerClass(UITableViewCell.self,forCellReuseIdentifier: "cell") self.DevicesTableView.dataSource = self

编辑:

我想显示一个在我的程序开头没有初始化的数组。它在用户搜索后包含一些BLE设备。

2 个答案:

答案 0 :(得分:0)

表视图有一个reloadData方法可以满足你的需要。

答案 1 :(得分:0)

非常简单

  1. 更新数组中的数据
  2. 然后使用重新加载您的tableview your_tableview.reloadData()
  3. 例如:

    var array = ["one","two","Three","Four"] // your array will take no.of.row in section
    DevicesTableView.reloadData()