arrays - 对象数组和 TableView

标签 arrays swift uitableview

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

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/

相关文章:

ios - SwiftUI 中 UISplitViewController 的等价物是什么

ios - 如何将数据从 Collection View 单元格传递到新的 View Controller (以编程方式且无需 Storyboard)?

iphone - UITableView 垂直和水平滚动

java - 数组引用

ios - Swift 4.2 中自定义类 UIButton 的错误。 UIButton 有什么新功能?

javascript - 为什么这个函数不能处理这个特定的数组?

ios - 如何在选择器中传递自定义 UITableViewCell

iphone - 将 UITabBarItem 添加到 UIViewController 中的 UITabBar

c++ - 程序编译但偶尔崩溃引用原因 255 (CodeBlocks)

c# - 检查数组成员是否存在