在UITableView中选择所有单元格

sat*_*ish 14 iphone

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

小智 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

    }
}
Run Code Online (Sandbox Code Playgroud)


Ema*_*mad 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];
        }
    }
Run Code Online (Sandbox Code Playgroud)


Vla*_*mir 8

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

[menuTable selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];
Run Code Online (Sandbox Code Playgroud)

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

  • 如果表视图允许多个选择,则可以选择多个单元格. (2认同)