ios - 在 swift 3 中出列可重用单元的正确方法(最佳实践)是什么?

标签 ios swift uitableview

我想将一个单元格出列以便在 TableView 中使用,但我担心我做错了。

目前我是这样做的:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "CellID", for: indexPath) as? CustomCell else {
        return UITableViewCell()
    }

    //Configure the atributes here.            

    return cell
}

我真正想知道的是它是否正确,或者我必须检查单元格是否为零?

var cell = tableView.dequeueReusableCell(withIdentifier: "CellID", for: indexPath) as CustomCell

if cell == nil {
    cell = CustomCell()
}

哪些代码是正确的?如果他们都不是正确的巫婆方式是正确的方式?

最佳答案

只是强制打开单元格:

let cell = tableView.dequeueReusableCell(withIdentifier: "CellID", for: indexPath) as! CustomCell
...
return cell

由于在 Interface Builder 中设计了单元格,因此崩溃将揭示设计错误。

关于ios - 在 swift 3 中出列可重用单元的正确方法(最佳实践)是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43413705/

相关文章:

ios - 如何在 Xamarin 中实现文档提供程序扩展?

ios - 如何在 Swift 中创建动态字符串?

ios - 为什么 Swift 2 不读取这个 iOS 文件?

objective-c - 检测自定义表格单元格内多个 UI View 的点击/触摸的理想方法是什么?

ios - 在 dealloc 之前观察并删除类别中的 NSNotification

ios - 如何过滤获取的数据并将其加载到 TableView 中?

ios - 启动时应用程序崩溃 iOS 7 崩溃日志解释

ios - 如何使 UITableView 标题可选择?

swift - 如何在不打印两次数据的情况下重新加载 UITableView?

iphone - 不会调用 canMoveRowAtIndexPath - 但会调用 canEditRowAtIndexPath