gpt4 book ai didi

ios - 创建数组并将元素分配给 UITableViewCell 并取回 labelText 元素时出现问题

转载 作者:行者123 更新时间:2023-11-29 02:33:06 24 4
gpt4 key购买 nike

正如标题所说,我有这个代码:

// Playground - noun: a place where people can play

import UIKit

var placesTableCells:[UITableViewCell] = []
var temporalCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
for i in 0...10 {
temporalCell?.textLabel?.text = "ola k ace \(i)"
placesTableCells.append(temporalCell!)
println(placesTableCells[i].textLabel!.text!)
}
println()
for i in 0...10 {
println(placesTableCells[i].textLabel!.text!)
}

当我在 for 循环中请求 placesTableCells 时一切正常,它打印:

ola k ace 0
ola k ace 1
ola k ace 2
ola k ace 3
ola k ace 4
ola k ace 5
ola k ace 6
ola k ace 7
ola k ace 8
ola k ace 9
ola k ace 10

但是当我从中请求数组时,它只返回“ola k ace 10”十次,它打印:

ola k ace 10
ola k ace 10
ola k ace 10
ola k ace 10
ola k ace 10
ola k ace 10
ola k ace 10
ola k ace 10
ola k ace 10
ola k ace 10
ola k ace 10

问题出在哪里?

最佳答案

我相信这样做的原因是您在 for 循环之外声明了 temporalCell,并且您每次在循环中不断更改它的值,这也会更改数组中先前引用对象的值。

如果您希望在数组中添加不同的对象,请在 for 循环中移动您的 temporalCell 声明

var placesTableCells:[UITableViewCell] = []
for i in 0...10 {
var temporalCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
temporalCell.textLabel?.text = "ola k ace \(i)"
placesTableCells.append(temporalCell)
println(placesTableCells[i].textLabel!.text!)
}
println()
for i in 0...10 {
println(placesTableCells[i].textLabel!.text!)
}

它应该可以工作。让我知道。

关于ios - 创建数组并将元素分配给 UITableViewCell 并取回 labelText 元素时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26668295/

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