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, I get a list of selected UITableView rows. Is there an easy way to get a list of rows that are NOT selected?

Thank you for your help.

+4
source share
5 answers

There may be elegant solutions, but it 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: according to 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]; } } } 
+10
source
 NSMutableArray *a = [NSMutableArray array]; // that 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]; } } } 
+2
source

You can use the return value of numberOfRows minus the total selected rows.

0
source
 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
source

I have added some extra lines to the Anupdas code. He certainly deserves all the credit.

  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]; } 
0
source

Source: https://habr.com/ru/post/1469158/


All Articles