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.
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];
}
}
}
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];
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With