我希望能够在选择单元格时突出显示我的自定义单元格文本标签目前我可以突出显示它们,但是当我向下滚动表格时,我没有选择的其他几个单元格标签也被突出显示。
我知道这可能与出队可重复使用的单元格有关,但我无法修复它。
我曾尝试使用 didSelectRowAt 函数方法,我还尝试允许我的 tableview 进行多项选择并在我的 customCell 类中使用 setSelected 函数
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! RecommendationCell
if cell.isSelected {
if cell.label.textColor == UIColor.highlightColor {
cell.label.textColor = .white
} else {
cell.label.textColor = UIColor.highlightColor
}
}
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
if selected {
self.label.textColor = UIColor.highlightColor
}
}