在UITableView中选择所有单元格

时间:2009-10-06 07:16:45

标签: iphone

当用户按下工具栏中的按钮时,选择表格中所有单元格(UITableView)的最佳方法是什么?

4 个答案:

答案 0 :(得分:29)

这是我遍历表格的方法,效果很好。

for (int i = 0; i < [ptableView numberOfSections]; i++) {
    for (int j = 0; j < [ptableView numberOfRowsInSection:i]; j++) {
        NSUInteger ints[2] = {i,j};
        NSIndexPath *indexPath = [NSIndexPath indexPathWithIndexes:ints length:2];
            UITableViewCell *cell = [ptableView cellForRowAtIndexPath:indexPath];
           //Here is your code

    }
}

答案 1 :(得分:19)

这可以选择表视图中的所有行:

for (NSInteger s = 0; s < self.tableView.numberOfSections; s++) {
        for (NSInteger r = 0; r < [self.tableView numberOfRowsInSection:s]; r++) {
            [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:r inSection:s]
                                        animated:NO
                                  scrollPosition:UITableViewScrollPositionNone];
        }
    }

答案 2 :(得分:8)

您可以选择一个单元格调用表视图的selectRowAtIndexPath方法:

[menuTable selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];

但是,您无法在UITableView中选择多个单元格。 如果要显示和处理单元格的开/关状态,则应使用具有UITableViewCellAccessoryCheckmark类型的附件视图的单元格。 (有关详细信息,请参阅文档)

答案 3 :(得分:3)

我自己没试过,但试试这个:

在你的按钮操作中,循环遍历indexPath并调用它:

for (i = 0; i < [tableView numberOfSections]; i++) {
    for (j = 0; j < [tableView numberOfRowsInSection:i]; j++) {
         indexPath.row = j;
         indexPath.section = i;
         [tableView selectRowAtIndexPath:indexPath animated:animated scrollPosition:scrollPosition];
    }
}