user3106462
user3106462

Reputation: 189

Deselect invisible row

I have problem with updating rows in UITableView. In my tableView only one row in section can be selected (there can be a couple of section). So, when user select row, I programmatically deselect other rows in this section. Everything works when deselected rows are visible. If any deselected row isn't visible, then it is still checked. But didDeselectRowAtIndexPath is called for this row. Why? How to fix this?

- (NSIndexPath*)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    // It is predefined group - only one row in section cab be selected
    // Deselect other rows in section
    for (NSIndexPath * selectedIndexPath in tagTableView.indexPathsForSelectedRows) {
        if (selectedIndexPath.section == indexPath.section) {
            [self tableView:tableView didDeselectRowAtIndexPath:selectedIndexPath];
            [tableView deselectRowAtIndexPath:selectedIndexPath animated:YES];
        }
    }

    return indexPath;
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tagTableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
}

Upvotes: 2

Views: 276

Answers (1)

rckoenes
rckoenes

Reputation: 69459

Selected state of a cell should not saved in the UITableViewCell but in the object (model) that fills the cell.

Since tablecells are reused there state are representing the the last state of the model that filled them. You should either keep the selected state in the object filling the cell of keep an array of select indexPaths.

The in the -tableView:cellForRowAtIndexPath: set the correct state of that cell. If you model object hold the selected state you can just set the state according to that property or if you are keeping an array with select indexpaths check if the path is in the array and set the state accordingly.

Upvotes: 1

Related Questions