2013-03-14 28 views
4

我有以下代碼。UITableView行未被選中的列表

NSMutableArray *aList = [[NSMutableArray alloc] init]; 
for (NSIndexPath *indexPath in tableview1.indexPathsForSelectedRows) { 
    NSString *r = [NSString stringWithFormat:@"%i",indexPath.row]; 
    [aList addObject:r]; 
} 

所以我得到的是被選中的UITableView行列表。是否有一個簡單的方法獲得列表的行是不是選擇?

謝謝你的幫助。

回答

10

有可能是優雅的解決方案,但這將工作。

NSMutableArray *allIndexPaths = [@[]mutableCopy]; 
    NSInteger nSections = [self.tableView numberOfSections]; 
    for (int j=0; j<nSections; j++) { 
     NSInteger nRows = [self.tableView numberOfRowsInSection:j]; 
     for (int i=0; i<nRows; i++) { 
      NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:j]; 
      [allIndexPaths addObject:indexPath]; 
     } 
    } 

    NSArray *selectedIndexPaths = self.tableView.indexPathsForSelectedRows; 

    for (NSIndexPath *indexPath in selectedIndexPaths) { 
     [allIndexPaths removeObject:indexPath]; 
    } 

    NSArray *unselectedIndexPaths = [NSArray arrayWithArray:allIndexPaths]; 

編輯:按照Rikkles建議

NSMutableArray *unselectedIndexPaths = [@[]mutableCopy]; 
NSArray *selectedIndexPaths = self.tableView.indexPathsForSelectedRows; 
NSInteger nSections = [self.tableView numberOfSections]; 
for (int j=0; j<nSections; j++) { 
    NSInteger nRows = [self.tableView numberOfRowsInSection:j]; 
    for (int i=0; i<nRows; i++) { 
     NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:j]; 
     if (![selectedIndexPaths containsObject:indexPath]) { 
      [unselectedIndexPaths addObject:indexPath]; 
     } 
    } 
} 
+0

謝謝。看起來你的代碼並沒有給我所需要的東西。 – 2013-03-14 07:43:48

+0

@TBlue你現在可以檢查,我做了編輯 – Anupdas 2013-03-14 08:47:38

+0

謝謝。它似乎接近我所需要的,除了我需要的是不是indexPaths的行列表,你知道嗎? – 2013-03-14 09:23:57

0

您可以使用numberOfRows返回值減去總的選定行。

+0

要得到什麼!? - 未選擇的行數? – 2013-03-14 07:35:05

0
NSMutableArray * yourNewArr = [[NSMutableArray alloc ] init]; 

for(NSUInteger i = 0 ; i < [yourArr count] ; i++){ 

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0]; 

    NSArray *selIndexs = self.tableView.indexPathsForSelectedRows; 

    if(![selIndexs containsObject:indexPath]){ 

     [yourNewArr addObject:indexPath];       
    } 
} 

NSLog(@"%@", yourNewArr); // this will contain unselected index paths. 
+0

謝謝。什麼是loopPath? – 2013-03-14 07:46:56

+0

sry ..這是indexPath - 錯字! – 2013-03-14 07:52:32

+0

它應該不是如果(![selIndexs containsObject:indexPath])? – Pfitz 2013-03-14 08:02:45

2
NSMutableArray *a = [NSMutableArray array]; // that's all unselected indexPaths 
NSArray *l = [self.tableView indexPathsForSelectedRows]; 
NSIndexPath *idx; 
for (NSUInteger sec=0; sec<[self.tableView numberOfSections]; sec++) { 
    for (NSUInteger r=0; r<[self.tableView numberOfRowsInSection:sec]; r++) { 
    idx = [NSIndexPath indexPathForRow:r inSection:sec]; 
    if(![l containsObject:idx]){ 
     [a addObject:idx]; 
    } 
    } 
} 
+0

謝謝。但是我無法讓你的代碼工作。在你有[self.tableView indexPathForRow:r inSection:sec]的行發生錯誤。 – 2013-03-14 09:50:32

+0

有一個失蹤';'在那條線上。道歉。 – Rikkles 2013-03-14 09:58:26

+0

對不起。我認爲這是第二行。 – 2013-03-14 10:09:00

0

我已經添加了一些額外的行Anupdas'代碼。他當然值得整個信貸。

NSMutableArray *allIndexPaths = [@[]mutableCopy]; 
    NSInteger nSections = [tableview1 numberOfSections]; 
    for (int j=0; j < nSections; j++) { 
     NSInteger nRows = [tableview1 numberOfRowsInSection:j]; 
     for (int i=0; i<nRows; i++) { 
      NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:j]; 
      [allIndexPaths addObject:indexPath]; 
     } 
    } 
    NSArray *selectedIndexPaths = tableview1.indexPathsForSelectedRows; 
    for (NSIndexPath *indexPath in selectedIndexPaths) { 
     [allIndexPaths removeObject:indexPath]; 
    } 

    NSMutableArray *bList = [[NSMutableArray alloc] init]; 
    for (NSInteger k = 0; k < allIndexPaths.count; k++) { 
     NSString *r = [NSString stringWithFormat:@"%i",[[allIndexPaths objectAtIndex:k] row]]; 
     [bList addObject:r]; 
    } 
相關問題