如何返回循环的每个元素。我的意思是我想显示这个循环的每个名称到行文本。它只返回最后一个元素。我怎样才能归还这一切?
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellFullname = tableview.dequeueReusableCell(withIdentifier: "cell", for: indexPath as IndexPath)
for last in lastnames {
cellFullname.textLabel?.text = "\(last)"
}
return cellFullname
}
最佳答案
您不需要循环来显示 UITableView
中的元素
假设您有一个姓氏数组:
var lastnames = ["....
并且您想将每个元素放入一个 UITableViewCell 中。两步:
定义您需要的细胞数量:
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return lastnames.count }
使用以下名称更新
UITableView
:func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cellFullname = tableview.dequeueReusableCell(withIdentifier: "cell", for: indexPath as IndexPath) cellFullname.textLabel?.text = lastnames[indexPath.row] return cellFullname }
关于ios - 如何将循环的每个元素显示到表格行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40740135/