在编辑模式下更新单元的附件

Ben*_*ard 3 iphone cocoa-touch uitableview ios

我有一个相当普通的分组UITableView,允许用户选择一个部门.当他们选择一行时,我更新附件视图以在新行上显示复选标记并从上一行中删除它.

我还允许编辑表格视图.在编辑模式下,隐藏复选标记,这很好.但是,如果用户删除当前所选(已选中)部门的行,我需要以编程方式移动复选标记.

我尝试使用与使用新部门时添加和删除复选标记相同的方法:

- (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];
}
Run Code Online (Sandbox Code Playgroud)

但是当表视图存在编辑模式时,没有行具有复选标记附件.我甚至尝试在编辑模式下手动重新加载行(如上面的代码所示).

当表格视图退出编辑模式时,如何确保行的附件刷新?

rma*_*ddy 6

您应该使用accessoryType非编辑模式并editingAccessoryType用于编辑模式.这样,您无需清除复选标记或恢复进入和退出编辑模式的复选标记.设置editingAccessoryTypeUITableViewCellAccessoryNone.

当然,您需要确保cellForRowAtIndexPath:始终accessoryType为每一行设置正确的.

编辑:

重写setEditing:animated:将允许您在离开编辑模式时重新加载已检查的行(如果可见).