正如标题所示,我有以下代码:
// 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 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
问题在哪里?
发布于 2014-10-31 05:57:36
我相信这是因为您在for循环之外声明了temporalCell,并且每次在循环中都会更改它的值,这也会改变数组中以前引用对象的值。
如果希望在数组中添加不同的对象,请将temporalCell声明移到for循环中,如下所示
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!)
}
而且它应该能工作。让我知道。
https://stackoverflow.com/questions/26668295
复制相似问题