[英]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.