2016-10-08 94 views
0

我喜歡在用戶觸摸blue中的單元格時突出顯示錶格單元格中的行,然後當用戶選擇操作按鈕時,我希望它將其更改爲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

回答

1

鑑於控制器初始化:

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設置

+0

1.我的細胞是清晰的彩色2.當用戶敲擊它們變成藍色3.當用戶按動作按鈕,他們變成灰色,保持灰色。我希望你現在清楚。對不起,我感到困惑 – Coder221

+0

我現在明白了:)我最近的編輯應該做的伎倆。關鍵是引用cellForRow中的「被選中」狀態來確定背景顏色。 – Lytic

+0

欣賞響應。你有沒有關於你正在說話的步驟的簡短示例代碼,這將對我有很大幫助。 – Coder221

相關問題