Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating a cell's accessory in edit mode

I have a fairly commonplace grouped UITableView that allows a user to select a department. When they select a row, I update the accessory view to display a checkmark on the new row and remove it from the previous row.

I also allow editing of the table view. In edit mode, the checkmarks are hidden, which is fine. But if the user deletes the row for the currently selected (checked) department, I need to programmatically move the checkmark.

I have tried using the same approach that I use to add and remove the checkmarks when a new department is used:

- (void)deleteDepartmentAtIndex:(NSInteger)index
{
    //if the current item is using the deleted department, move the checkmark
    Department *dept = [self.departments objectAtIndex:index];
    if (self.item.department == dept)
    {
        UITableViewCell *noneCell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForItem:self.departments.count inSection:0]];
        noneCell.accessoryType = UITableViewCellAccessoryCheckmark;
        [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForItem:self.departments.count inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
    }

    //delete the department and save
    [dept.managedObjectContext deleteObject:dept];

    //delete the row
    [self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForItem:index inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
}

But when the table view exists edit mode, no row has the checkmark accessory. I have even tried manually reloading the row whilst still in edit mode (as seen in the code above).

How can I make sure that the row's accessory is refreshed when the table view exits edit mode?

like image 755
Ben Packard Avatar asked Feb 19 '23 07:02

Ben Packard


1 Answers

You should use accessoryType for non-edit mode and use editingAccessoryType for edit mode. This way you don't have to clear the checkmark or restore the checkmark going in and out of editing mode. Set the editingAccessoryType to UITableViewCellAccessoryNone.

And of course you need to make sure that your cellForRowAtIndexPath: always sets the proper accessoryType for each row.

Edit:

Overriding setEditing:animated: will allow you to reload the checked row (if visible) when leaving edit mode.

like image 105
rmaddy Avatar answered Feb 27 '23 04:02

rmaddy