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

时间:2012-12-02 03:46:25

标签: iphone ios cocoa-touch uitableview

我有一个相当普通的分组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];
}

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

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

1 个答案:

答案 0 :(得分:5)

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

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

编辑:

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

相关问题