gpt4 book ai didi

ios - 如何在 Swift 中填充 TableView ?

转载 作者:行者123 更新时间:2023-11-30 14:02:39 25 4
gpt4 key购买 nike

我写了这段代码:

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

所以代码运行得很好,并向我显示了填充的表格 View :

第 1 行“一”第 2 行“二”

但是我怎样才能不在程序开始时而是在其他任何时候填充TableView呢?我尝试在 viewDidLoad 的其他函数中调用下面的方法,但它不起作用......为什么?

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

编辑:

我想显示一个在程序开始时未初始化的数组。用户搜索到后,其中包含一些BLE设备。

最佳答案

很简单

  1. 更新数组中的数据
  2. 然后使用以下命令重新加载您的表格 View your_tableview.reloadData()

例如:

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

关于ios - 如何在 Swift 中填充 TableView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32799098/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com