2010-11-30 45 views
13

我在我的應用程序中有多個UITableViews,有沒有一種方法來檢測用戶在該列中選擇了哪個單元格/行?UITableView檢測選定的細胞?

也有可能以編程方式取消選擇一個單元格/行?

謝謝。

回答

30

獲取當前選擇的索引路徑表:

NSIndexPath *path = [tableView indexPathForSelectedRow]; 

取消當前所選行:

[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES]; 
2
NSIndexPath *path = [tableView indexPathForSelectedRow]; 

上面一行將拋出EXC錯誤訪問如果沒有賣的是選擇,以便跟蹤與NSIndexPath實例變量在所選單元格中,只有取消當實際上是選用isSelected屬性的單元格。

UITableViewCell *cell = [tableView cellForRowAtIndexPath:someIndexPath]; 
if(cell.isSelected) { 
    [tableView deselectRowAtIndexPath:someIndexPath animated:YES]; 
}