code
override func tableView(_ tableView: UITableView,
cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "basicStyleCell", for: indexPath)
tableView.rowHeight = 200
cell.textLabel!.text = "\(indexPath.section), \(indexPath.row)"
if indexPath.row == 0 {
cell.backgroundColor = .red
}
return cell
}
else 문을 이용하여 row가 0이 아닐 경우 white로 바꿔주는 방법도 있지만
prepareForReuse를 이용하는 방법도 있다.
TableViewCell에 prepareForReuse 메서드를 사용하여 재사용되는 셀의 속성을 초기화해주면 된다.
extension TableViewCell {
override func prepareForReuse() {
super.prepareForReuse()
backgroundColor = .white
}
}