ios - 自定义 UITableViewCell 项目无法正确显示

标签 ios xcode swift uitableview uistoryboard

我当前的问题是这样的:

我的目标是为我的 iPhone 应用程序创建一个“主页” View 。它需要由一个“特色” View (所附图片 View 的顶部)和其下方的提要样式表格 View 组成:

enter image description here

到目前为止,我设计它的方式是使用具有两个不同单元原型(prototype)的单个表格 View :一个用于“特色” View (内部带有 ScrollView 和页面控件的深蓝色大单元),另一个用于“特色” View (带有 ScrollView 和页面控件的大深蓝色单元)对于“提要项目” View (当前下方为空单元格)。两者都附加了单独的类文件。我希望“特色”单元格仅在 View 顶部显示一次,然后是“提要项目”单元格,所有单元格都由来自外部后端的内容提供。

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
    }
}

但是使用上面的代码,我得到的只是 TableView 中的一堆空白行。

所以问题如下:

  1. 我应该怎样做才能生成我设计的单元格?和
  2. 这种方法是否正确,或者是否有更好的方法 处理这个问题吗?

最佳答案

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

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
}

关于ios - 自定义 UITableViewCell 项目无法正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37591171/

相关文章:

android - XE8 Twebbrowser 本地 img 文件

ios - NSUserDefaults 未在 Apple Watch 上更新

iphone 6 的 iOS 背景图片

ios - 本地化自定义 SiriKit 意图

ios - CALayer 位置包含 NaN : [nan -__]

swift - 在 if 语句中更改计时器的时间间隔

iphone - InAppSettingsKit ARC 替代方案

c# - 我应该为图像和颜色使用公共(public)静态字段吗?

ios - 如何正确实现 siri 快捷方式

ios - 无法将类型 'NSTaggedPointerString' 的值转换为 'NSArray'