ios - 如何在 UITableView 中显示 2 个自定义单元格

标签 ios swift uitableview custom-cell

我必须在表格中显示 2 个不同的单元格。我已经通过将原型(prototype)设置为表格来尝试过。但它仍然显示 Prototype table cells must have reuse identifiers 警告。

有人可以指导我解决此警告。

点击此链接: UITableview with more than One Custom Cells with Swift

最佳答案

在 Storyboard 中,您必须为单元格定义标识符,如下图 enter image description here

然后在 cellForRowAtIndexPath 中,您必须像这样为特定的单元格使用特定的标识符

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if indexPath.row == 0 {
        let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Identifier1")
        //set the data here
        return cell
    }
    else if indexPath.row == 1 {
        let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Identifier2")
        //set the data here
        return cell
    }
}

关于ios - 如何在 UITableView 中显示 2 个自定义单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39515763/

相关文章:

ios - iOS 中无法加载 Facebook 原生广告?

iOS开发应用程序启动崩溃

iphone - iOS:UITableViewCell 类中的按钮调用主 UIViewController 上的方法

ios - dyld : Library not loaded: @rpath/App. 框架/应用程序

ios - 如何防止人们从 Parse 核心数据请求中获取应用程序 ID 和客户端 ID?

ios - How to Solve Compiling "_OBJC_CLASS_$_xxxx", 引用自"Issue

ios - 从 UITableView 单元格内的 url 异步加载图像 - 滚动时图像更改为错误图像

ios - 今日小部件扩展高度 - iOS10

swift - 无法快速打印数组的值

ios - 在 UITableViewCell 内动画 UILabel 文本更改