我有一个像这样的对象数组:
var Dogs : [Dog] = [Dog]()
举个例子,打印时它看起来像这样:
[Dog {
name = Dog1;
age = 9;
}, Dog {
name = Dog2;
age = 4;
}]
我想在表格 View 中显示这个元素,所以我使用这个经典函数:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
cell.titleLabel.text = Dogs[indexPath.row]["name"]! as? String
return cell
}
但是我收到错误:
fatal error: Array index out of range
最佳答案
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
let dog = Dogs[indexPath.row]
cell.titleLabel.text = dog.name
return cell
}
关于arrays - 对象数组和 TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36754010/