4

次のコードがあります。

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

だから私が得るのは、選択された UITableView 行のリストです。選択されていない行のリストを取得する簡単な方法はありますか?

ご協力ありがとうございました。

4

5 に答える 5

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];
        }
    }
}
于 2013-03-14T07:33:45.847 に答える
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];
    }
  }
}
于 2013-03-14T09:03:55.270 に答える
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.
于 2013-03-14T07:38:07.437 に答える
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];
    }
于 2013-03-14T09:45:55.650 に答える