首页 > 解决方案 > 将数组元素及其索引设置为标签

问题描述

嗨,我有一个元素数组

var prices = ["100", "200", "90", "", "", "300", "100"]

和方法 cellForRowAt

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cellid") as? PriceViewCell
        for (index, item) in prices.enumerated() {
                    cell?.priceLbl.text = "Item \(index): \(item)"
                }
        return cell ?? UITableViewCell()
    }

输出是这样的:

在此处输入图像描述

但是首选结果是这样的(标记为红色):

在此处输入图像描述

标签: iosarraysswifttableview

解决方案


我将Joakim Danielson的答案放入一个问题中,以便未来的用户可以更轻松地找到它:

cell?.priceLbl.text = "Item \(indexPath.row + 1): \(prices[indexPath.row])"

推荐阅读