解决UITableViewCell的重用机制导致的问题
2019-07-22 本文已影响0人
雷霸龙
// 给每个cell打上不同tag标记
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let identifier = String(format: "%ld%ld%@", indexPath.section, indexPath.row, "SelectedBusinessCell")
var cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath)
if cell == nil {
cell = SelectedBusinessCell.init(style: .default, reuseIdentifier: identifier)
}
cell?.backgroundColor = UIColor.white
cell?.selectionStyle = UITableViewCell.SelectionStyle.none
return cell!
}
// 取消重用机制
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let identifier = "SelectedBusinessCell"
// var cell : SelectedBusinessCell? = tableView.dequeueReusableCell(withIdentifier: identifier) as? SelectedBusinessCell
var cell = tableView.cellForRow(at: indexPath) as? SelectedBusinessCell
if cell == nil {
cell = SelectedBusinessCell.init(style: .default, reuseIdentifier: identifier)
}
cell?.backgroundColor = UIColor.white
cell?.selectionStyle = UITableViewCell.SelectionStyle.none
return cell!
}