2017-06-13 61 views
1

我在Master-Detail設置的Master中有一個UITableView。用戶可以通過UITableViewRowAction與單元進行交互,也可以使用給定的搜索欄進行搜索。檢查在UITableViewCell中取消選擇

當用戶與搜索欄進行交互或將單元格向左滑動時,單元格將被取消選中。當一個單元格被取消選中時,我需要得到通知。

我試過func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath),但它似乎沒有迴應像這些的情況。

override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { 
     print("Cell Deselected") 
} 

這從來沒有在我上面給出的條件執行,但只有當tableView不同項目被選中它並得到執行,這不是我所需要

+0

您是否正確設置了您的代表? – gEeKyMiNd

+0

是的。我已經修改了這個問題更加清楚。 – Berry

+0

你在調用'tableView.deselectRow(at:indexPath,animated:true)'? – CarpenterBlood

回答

0

較新的答案:

你需要在其他響應者在表格之外選擇來抓。

例如,for going into edit mode

func setEditing(_ editing: Bool, animated: Bool) { 
    print("Cell deselected") 
} 

going into the search bar,使用此委託方法:

func searchBarShouldBeginEditing(UISearchBar) 
{ 
    print("Cell Deselected") 
} 

原來的答覆:

請務必將您的視圖控制器(或任何反對你的didDeselectRowAt功能居住)作爲表視圖的delegate。您可以在故事板中設置它,也可以通過編程方式進行設置。

+0

委託設置正確。當您選擇不同的項目時,取消選擇只會被調用,而不會在執行reloadData()或「UITableViewRowAction」 – Berry

0

你可以保持在一流水平的SelectedIndexPath,並在用戶表以外的任何地方倒是可以調用這個方法是這樣的: - :

tableView.deselectRow(at: SelectedIndexPath, animated: true) 

您也可以從接收呼叫

func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
} 
相關問題