【发布时间】:2016-10-04 17:09:14
【问题描述】:
当我选择一行时我有一个 tableView 我想在底部(tableView 之外)显示一个按钮,当我没有选择行时我希望它被设置为禁用(isEnabled = false。我怎样才能最好地处理这个?
目前我有这个逻辑,但是当我尝试选择多行时,这给我带来了问题。因为第一个选择会启用它,第二个会禁用它。
代码如下:
我在顶部的 var 中存储一个 true 或 false,名为:var someRowSelected = false
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedCell = tableView.cellForRow(at: indexPath)
selectedCell?.contentView.backgroundColor = UIColor(red: 84.0 / 255, green: 199.0 / 255, blue: 252.0 / 255, alpha: 1)
if(!someRowSelected){
self.someRowSelected = true
nextBtn.isEnabled = true
nextBtn.backgroundColor = UIColor(red: 84.0 / 255, green: 199.0 / 255, blue: 252.0 / 255, alpha: 1)
}else{
self.someRowSelected = false
nextBtn.isEnabled = false
nextBtn.backgroundColor = UIColor.darkGray
}
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
let selectedCell = tableView.cellForRow(at: indexPath)
selectedCell?.textLabel?.textColor = UIColor.darkGray
if(!someRowSelected){
self.someRowSelected = true
nextBtn.isEnabled = true
nextBtn.backgroundColor = UIColor(red: 84.0 / 255, green: 199.0 / 255, blue: 252.0 / 255, alpha: 1)
}else{
self.someRowSelected = false
nextBtn.isEnabled = false
nextBtn.backgroundColor = UIColor.darkGray
}
}
我很好奇是否有人可以帮助我。
谢谢! :)
【问题讨论】:
标签: ios swift xcode uitableview