我喜欢在用户触摸 blue
单元格时突出显示 tableview 单元格中的行,然后当用户选择操作按钮时,我希望它将其更改为 gray
颜色
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
var newCell = tableView.cellForRow(at: indexPath)!
newCell.backgroundColor = UIColor.blue
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let okButton = UIAlertAction(title: "Ok", style: .default, handler: { (action) -> Void in
print("Ok button tapped")
var newCell = tableView.cellForRow(at: indexPath)!
newCell.backgroundColor = UIColor.gray
})
let cancelButton = UIAlertAction(title: "Cancel", style: .cancel, handler: { (action) -> Void in
print("Cancel button tapped")
var newCell = tableView.cellForRow(at: indexPath)!
newCell.backgroundColor = UIColor.gray
})
alertController.addAction(okButton)
alertController.addAction(cancelButton)
present(alertController, animated: true, completion: nil)
}
func tableView(_ tableView: UITableView, didhighlightRowAt indexPath: IndexPath) {
var newCell = tableView.cellForRow(at: indexPath)!
newCell.backgroundColor = UIColor.clear
}
我这样做了,它突出显示了 white
颜色的单元格,点击操作按钮后,它仍然保持 white
但是当我点击另一个单元格时,前一个单元格变成并保持灰色
最佳答案
在 View Controller 中初始化:
NSMutableSet *selectedRows = NSMutableSet.alloc.init;
NSIndexPath *lastSelected = nil;
在 TableView 初始化中:
tableview.allowsMultipleSelection = false
在选择行中:
火警(你已经有了这个),lastSelected = indexPath;
,[selectedRows addObject:indexPath.row]
。
在行的单元格中:
cell.selectionStyle = UITableViewSelectionStyleBlue;
if ([selectionSet contains:indexPath.row])
cell.backgroundColor = UIColor.grayColor;
else
cell.backgroundColor = UIColor.clearColor;
在警报 View 委托(delegate)中:
[tableview deselectRowAtIndexPath:selectedIndexPath]
和 [tableview reloadIndexPath:lastSelection]
这应该: 1) 从清除所有单元格开始
2) 默认高亮点击时单元格变为蓝色
3) 选择警报 View 后重绘单元格
4) 如果单元格在 selectedRow 集合中,它将以灰色重绘
关于ios - 点击时突出显示表格 View 单元格,按下警报 Controller 操作按钮时取消突出显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39938256/