2011-01-06 24 views
15

選擇多行,我想選擇一個UITableView多行就像蘋果不會在本地報警應用程序(見圖片alt text在UITableView的

我怎麼能這樣做?

所有問題的答案,我看到到目前爲止是舊的(並且說「不能做」)或者在本地郵件應用程序中引用多行選擇。這種方法不是我想要的,因爲你必須先輸入「編輯」模式。希望能夠立即選擇行。

希望有人能幫助。

基督教

回答

24

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

if([tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark){ 
     [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone; 
    }else{ 
     [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark; 
    } 

編輯:

您還可以使用映射選中的單元陣列和的cellForRowAtIndexPath你要驗證是否accessoryType應檢查與否。

+6

你不應該在表格單元本身存儲檢查狀態 - 當表格滾動時它們可能會重複使用多行,你需要以某種方式恢復檢查狀態...... – Vladimir 2011-01-06 15:24:03

+0

我也有一個映射的檢查細胞在指數路徑小區行我還是驗證,如果accessoryType應檢查不 – 2011-01-06 15:28:38

8

時鐘應用程序中的「選定」行是具有UITableViewCellAccessoryCheckmark附件類型的單元格。

可能的步驟來實現是:

  1. 商店檢查每行/未選中狀態的地方。
  2. cellForRowAtIndexPath:方法,用於檢查行組小區的accessoryType屬性小區UITableViewCellAccessoryCheckmark
  3. cellForRowAtIndexPath:在未選中行集小區的accessoryType屬性小區UITableViewCellAccessoryNone
  4. 當選擇小區(抽頭)取消選擇,顛倒其檢查狀態和重裝細胞在該索引路徑
30

最簡單的方法我已經找到了:通過調用

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark; 
} 

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { 
    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone; 
} 

你可以看到選擇哪些細胞:

NSArray *selectedCells = [self.tableView indexPathsForSelectedRows]; 
+0

你的答案是真棒.. +1,但我有一個小問題,一個數組..如何獲得價值來自NSArray。如果你可以請編輯你的答案謝謝 – GoCrazy 2012-09-19 15:39:50

11

播放與

tableView.allowsMultipleSelection

這救了我小時的麻煩。找到here

0

如果您使用的是故事板,只需單擊表視圖,然後在右側面板上的屬性檢查器中,將「選擇」屬性設置爲「多選」。我已經使用Xcode版本8.2.1驗證了這一點。