[英]Highlight tableview cell when tapped and unhighlight when alert controller action buttons are pressed
当用户触摸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
但是当我点击另一个单元格时,前一个单元格变成并保持gray
在视图控制器初始化中:
NSMutableSet *selectedRows = NSMutableSet.alloc.init;
NSIndexPath *lastSelected = nil;
在表视图初始化中: 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;
在警报视图委托中: [tableview deselectRowAtIndexPath:selectedIndexPath]
和[tableview reloadIndexPath:lastSelection]
这应该:1)首先清除所有单元格
2)默认点击突出显示时,将单元格变为蓝色
3)选择警报视图后重绘单元格
4)如果在selectedRow集合中,单元格将用灰色重新绘制
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.