从数组中循环tableview单元格标签文本

Posted

技术标签:

【中文标题】从数组中循环tableview单元格标签文本【英文标题】:looping tableview cell label text from an array 【发布时间】:2017-11-14 07:07:58 【问题描述】:

我有一个数组:

let BRIArray = ["ATM BRI", "Internet Banking BRI"]

然后我在 cellForRowAt 计算它:

let BRIArrayCount = BRIArray.count

然后我循环它:

for i in 0 ..< BRIArrayCount 
                print (i) //i will increment up one with each iteration of the for loop
                cell.lblBankPayment.text = "\(BRIArray[i])"
                print("BRIArray: \(BRIArray[i])")
            

但结果只是得到最后一个字符串并如下图所示循环:

如何纠正 cellForRowAt 中的循环以在单元格标签文本中显示所有数组字符串?

【问题讨论】:

【参考方案1】:

无需使用循环 而不是循环 只需使用

cell.lblBankPayment.text = "(BRIArray[indexPath.row])"

如果你想循环中的单个标签连接字符串中的所有文本然后设置它

让 str = "(str)(" ")(BRIArray[index])"

【讨论】:

如果我这样设置 cell.lblBankPayment!.text = BRIArray[indexPath.row],有时会出现致命错误:索引超出范围 你可以使用 if 语句检查 ex if BRIArray.count

以上是关于从数组中循环tableview单元格标签文本的主要内容,如果未能解决你的问题,请参考以下文章