TableView Cell reuse and unwanted checkmarks - this is killing me

后端 未结 1 977
广开言路
广开言路 2020-11-30 11:43


Apple\'s iOS TableView and cell reuse is killing me. I searched and searched and studied, but can\'t find good docs or good answers. The problem is that wh

相关标签:
1条回答
  • 2020-11-30 12:04

    cell reusing can be tricky but you have to keep 2 things in mind:

    • Use one identifier for one type of cell - Using multiple identifiers is really only needed when you use different UITableViewCell-subclasses in one table view and you have to rely on their different behaviour for different cells
    • The cell you reuse can be in any state, which means you have to configure every aspect of the cell again - especially checkmars / images / text / accessoryViews / accessoryTypes and more

    What you need to do is to create a storage for your checkmark states - a simple array containing bools (or NSArray containing boolean NSNumber objects respectively) should do it. Then when you have to create/reuse a cell use following logic:

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *reuseIdentifier = @"MyCellType";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
        if(cell == nil) {
            /* create cell here */
        }
        // Configure cell now
        cell.textLabel.text = @"Cell text"; // load from datasource
        if([[stateArray objectAtIndex:indexPath.row] boolValue]) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        } else {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
        return cell;
    }
    

    then you will have to react on taps:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        [stateArray replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:![[stateArray objectAtIndex:indexPath.row] boolValue]]];
        [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
    

    Just remember to use NSMutableArray for your data store ;)

    0 讨论(0)
提交回复
热议问题