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

您可以使用 numberOfRows 返回值减去所选行的总数。

于 2013-03-14T07:31:08.757 回答
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 回答