如何在 Swift 中填充表视图? [英] How to fill a Table View in 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
}
所以代码运行得很好,并向我展示了填充了以下内容的 tableview:
So the code works pretty well and shows me the tableview filled with:
第 1 行一个"第2行两个"
Row1 "One" Row2 "Two"
但是我怎样才能不在程序开始时而是在其他任何时候填充 TableView 呢?我试图在 viewDidLoad 的另一个函数中调用下面的方法,但它不起作用......为什么?
But how can I fill the TableView not at the beginning of my program but anytime else? I tried to call the methods below in an other function that viewDidLoad but it doest't work... why?
self.DevicesTableView.registerClass(UITableViewCell.self,forCellReuseIdentifier: "cell")self.DevicesTableView.dataSource = self
我想显示一个在程序开始时未初始化的数组.用户搜索后,其中包含一些 BLE 设备.
I would like to display an array which isn't initialized at the beginning of my program. It contains some BLE devices after the user searched for them.
推荐答案
很简单
- 更新数组中的数据
- 然后使用your_tableview.reloadData()
例如:
var array = ["one","two","Three","Four"] // your array will take no.of.row in section
DevicesTableView.reloadData()
这篇关于如何在 Swift 中填充表视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!