我当前的问题是这样的:
我的目标是为我的 iPhone 应用程序创建一个“主页” View 。它需要由一个“特色” View (所附图片 View 的顶部)和其下方的提要样式表格 View 组成:
到目前为止,我设计它的方式是使用具有两个不同单元原型(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 中的一堆空白行。
所以问题如下:
- 我应该怎样做才能生成我设计的单元格?和
- 这种方法是否正确,或者是否有更好的方法 处理这个问题吗?
最佳答案
由于您已经在 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/