我有一个相当普通的分组 UITableView
,它允许用户选择一个部门。当他们选择一行时,我更新附件 View 以在新行上显示复选标记并将其从上一行中删除。
我还允许编辑表格 View 。在编辑模式下,复选标记是隐藏的,这很好。但是,如果用户删除了当前选定(选中)部门的行,我需要以编程方式移动复选标记。
我尝试使用与使用新部门时添加和删除复选标记相同的方法:
- (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];
}
但是当表格 View 处于编辑模式时,没有行有复选标记附件。我什至尝试在仍处于编辑模式时手动重新加载该行(如上面的代码所示)。
如何确保在表格 View 退出编辑模式时刷新行的附件?
最佳答案
您应该在非编辑模式下使用accessoryType
,在编辑模式下使用editingAccessoryType
。这样您就不必清除复选标记或恢复进入和退出编辑模式的复选标记。将 editingAccessoryType
设置为 UITableViewCellAccessoryNone
。
当然,您需要确保您的 cellForRowAtIndexPath:
始终为每一行设置正确的 accessoryType
。
编辑:
重写 setEditing:animated:
将允许您在离开编辑模式时重新加载选中的行(如果可见)。
关于iphone - 在编辑模式下更新单元格的附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13666224/