对象数组和表视图

时间:2016-04-20 20:00:34

标签: arrays swift uitableview

我有一个像这样的对象数组:

var Dogs : [Dog] = [Dog]()

为了给你一个例子,当你打印它时它看起来像这样:

[Dog {
    name = Dog1;
    age = 9;

}, Dog {
    name = Dog2;
    age = 4;

}]

我想在Table 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

1 个答案:

答案 0 :(得分:0)

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
}
相关问题