我是 Swift 新手,我没有使用 Objective C 编码的经验。我面临的问题是,当我将类对象中的数据填充到表格视图单元格中时,所有单元格都显示相同的数据(最近访问的最后一个数组对象)。有人可以帮我吗?谢谢。
class TableViewController: UITableViewController{
var urgentCenters:Array<UrgentCenterDetails> = []
override func viewDidLoad() {
super.viewDidLoad()
self.loadInfo()
println("Number of entries: \(self.urgentCenters.count)")
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
return 1;
}
override func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
//println("Number of rows \(urgentCenters.count)")
return self.urgentCenters.count
}
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
let cellId = "cell"
var cell = tableView.dequeueReusableCellWithIdentifier(cellId) as? UITableViewCell
if nil==cell {
cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: cellId)
}
if let ip = indexPath{
cell!.textLabel.text = urgentCenters[indexPath.row].title
}
return cell
}
func loadInfo(){
// Append data to urgentCenters data
var center = UrgentCenterDetails()
var title:String
title = "Penn's Landing"
center.setCenterDetails(title)
urgentCenters.append(center)
title = "Liberty Bell"
center.setCenterDetails(title)
urgentCenters.append(center)
}
}