2015-09-26 100 views
0

我写了这个代码:如何在Swift中填充表格视图?

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

因此,代码工作得很好,并表明我充满了实现代码如下:

ROW1“一” 行2“二”

但我怎么能填写TableView不是在我的程序开始,但随时随地?我尝试在其他函数viewDidLoad中调用下面的方法,但它不起作用...为什么?

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

编辑:

我想显示未在我的节目的开始时被初始化的阵列。它包含用户搜索后的一些BLE设备。

回答

1

它非常简单

  1. 更新您的阵列中的数据
  2. 然后使用 your_tableview.reloadData()

防爆刷新你的实现代码如下:

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

表视图有一个reloadData方法,它可以做你想做的。