Select all the cells in UITableView

前端 未结 4 1214
孤独总比滥情好
孤独总比滥情好 2020-12-09 10:55

what would be the best method to select all the cells in the table(UITableView) when the user presses a button in the tool bar?

相关标签:
4条回答
  • 2020-12-09 11:50

    You can select a cell calling table view's selectRowAtIndexPath method:

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

    However you can't select multiple cells in a UITableView. If you want to show and process on/off state of cells you should use cell with accessory view with UITableViewCellAccessoryCheckmark type. (see docs for more details)

    0 讨论(0)
  • 2020-12-09 11:51

    Haven't tried it myself, but try this:

    In your button action, loop through the indexPath and call it:

    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];
        }
    }
    
    0 讨论(0)
  • 2020-12-09 11:56

    Here is my method to traverse a table, it works well.

    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
    
        }
    }
    
    0 讨论(0)
  • 2020-12-09 11:57

    This can select all rows in the table view:

    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];
            }
        }
    
    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题