gpt4 book ai didi

ios - 如何将循环的每个元素显示到表格行?

转载 作者:搜寻专家 更新时间:2023-11-01 06:17:43 25 4
gpt4 key购买 nike

如何返回循环的每个元素。我的意思是我想显示这个循环的每个名称到行文本。它只返回最后一个元素。我怎样才能归还这一切?

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 中。两步:

  1. 定义您需要的细胞数量:

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return lastnames.count
    }
  2. 使用以下名称更新 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/

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