自定义UITableViewCell项目无法正常显示

时间:2016-06-02 11:54:59

标签: ios xcode swift uitableview uistoryboard

我目前的问题是这样的:

我的目标是打造一个"家庭"查看我的iPhone应用程序。它需要包含一个"特色"视图(附加图片视图的顶部)和它下面的Feed样式表视图:

enter image description here

到目前为止,我设计它的方法是使用带有两个不同单元格原型的单个表视图:一个用于"特色" view(带有滚动视图和页面控件的大型深蓝色单元格)和一个用于" Feed项目" view(当前空单元格下方)。两者都附加了单独的类文件。我希望"特色"要在视图顶部只显示一次的单元格,然后是" Feed项目"细胞,都来自外部后端的内容。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    switch indexPath.row {
    case 0:
      let cell: FeaturedItemCell = FeaturedStockCell(style: .Default, reuseIdentifier: "featuredItem")
      return cell
    default:
      let cell: HomeItemCell = HomeItemCell(style: .Default, reuseIdentifier: "homeItem")
      return cell
    }
}

然而,使用上面的代码,我所得到的只是表视图中的一堆空白行。

所以问题如下:

  1. 在我设计细胞时,我该怎么做才能产生细胞?和
  2. 这种方法是正确的,还是有更好的方法 处理这个问题?

1 个答案:

答案 0 :(得分:0)

由于您已经在UITableView中创建了原型,因此实例化单元格的方法会有所不同。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var identifier: String
    switch indexPath.row {
    case 0:
        identifier = "featuredItem"
    default:
        identifier = "homeItem"

    }
    let cell: FeaturedItemCell = tableView.dequeueReusableCellWithIdentifier("featuredItem", forIndexPath: indexPath)
    return cell
}
相关问题