【发布时间】:2019-04-05 10:59:40
【问题描述】:
我有一个自定义的 UITableViewCell 类,如下所示:
class CustomTableViewCell: UITableViewCell {
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.selectionStyle = .none
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
print("Selected: ", selected)
if selected {
self.backgroundColor = .red
} else {
self.backgroundColor = .white
}
}
override func setHighlighted(_ highlighted: Bool, animated: Bool) {
super.setHighlighted(highlighted, animated: animated)
print("Highlighted: ", highlighted)
if highlighted {
self.backgroundColor = .red
} else {
self.backgroundColor = .white
}
}
然后在我的UITableViewDelegate 中添加了这段代码:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
// Navigate to the next screen
}
但是,print() 的两个陈述总是错误的。 backgroundColor 永远不会改变。我在这里做错什么了吗?
【问题讨论】:
-
你设置了tableview属性tableView.allowsSelection = true吗?
标签: ios swift uitableview uikit