我想将一个单元格出列以便在 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/