Reputation: 486
I'm want to dequeue a cell for use in the table view but I'm afraid that I'm doing it in the wrong way.
Currently I'm doing this way:
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
}
What I'm really want to know is if it is right or I have to check if the cell is nil?
var cell = tableView.dequeueReusableCell(withIdentifier: "CellID", for: indexPath) as CustomCell
if cell == nil {
cell = CustomCell()
}
What code are correct? If none of them are right witch way is the right way?
Upvotes: 0
Views: 576
Reputation: 285270
Just forced unwrap the cell:
let cell = tableView.dequeueReusableCell(withIdentifier: "CellID", for: indexPath) as! CustomCell
...
return cell
Since you have designed the cell in Interface Builder a crash will reveal a design error.
Upvotes: 2