iPad: Iterate over every cell in a UITableView?

后端 未结 7 1901
眼角桃花
眼角桃花 2020-12-08 19:37

iPad: Iterate over every cell in a UITableView?

7条回答
  •  执笔经年
    2020-12-08 20:15

    (This builds on aroths answer.)

    I like to define this as a category to UITableView so it's available everywhere.

    (As mentioned a few times, you should be sure you really want to iterate over the cells themselves. For example: I use this to clear the UITableViewAccessoryCheckmark's from all the cells before setting it to the user selected cell. A good rule of thumb is to do this only if the datasource methods can't do what you need to.)

    Define like this:

    - (void)enumerateCellsUsingBlock:(void (^)(UITableViewCell *cell))cellBlock {
        NSParameterAssert(cellBlock != nil);
        for (int section = 0; section < [self numberOfSections]; section++) {
            for (int row = 0; row < [self numberOfRowsInSection:section]; row++) {
                NSIndexPath *cellPath = [NSIndexPath indexPathForRow:row inSection:section];
                UITableViewCell *cell = [self cellForRowAtIndexPath:cellPath];
                if (cellBlock != nil) {
                    cellBlock(cell);
                }
            }
        }
    }
    

    Call like this:

    [self.tableView enumerateCellsUsingBlock:^(UITableViewCell *cell) {
        NSLog(@"cell:%@", cell);
    }];
    

    It would be good style to typedef the block, too.

提交回复
热议问题