Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of UITableView Rows That Are NOT Selected

I have the following code.

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

So what I get is a list of UITableView rows that are selected. Is there a simple way of getting a list of rows that are NOT selected?

Thank you for your help.

like image 803
El Tomato Avatar asked Dec 16 '22 13:12

El Tomato


2 Answers

There may be elegant solutions but this will work.

    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];

EDIT : As per Rikkles suggestion

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];
        }
    }
}
like image 167
Anupdas Avatar answered Dec 28 '22 07:12

Anupdas


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];
    }
  }
}
like image 26
Rikkles Avatar answered Dec 28 '22 08:12

Rikkles