我有以下代码。
NSMutableArray *aList = [[NSMutableArray alloc] init];
for (NSIndexPath *indexPath in tableview1.indexPathsForSelectedRows) {
NSString *r = [NSString stringWithFormat:@"%i",indexPath.row];
[aList addObject:r];
}
所以我得到的是一个选择的UITableView行列表。是否有一种简单的方法可以获得 选择的列表行?
感谢您的帮助。
答案 0 :(得分: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];
}
}
}
答案 1 :(得分: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];
}
}
}
答案 2 :(得分:0)
您可以使用numberOfRows返回值减去所选的总行数。
答案 3 :(得分: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.
答案 4 :(得分: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];
}