Ios UITableViewCellAccessoryCheckmark停留在tableView上

Ios UITableViewCellAccessoryCheckmark停留在tableView上,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我知道在行选择中,您可以使用以下代码显示复选标记: UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; cell.accessoryType = UITableViewCellAccessoryCheckmark; 如果用户在不同的视图控制器之间移动太多,如何使复选标记保留在选定的行上 感谢您将UITableViewController设置为NO 如果要在解除分配表视图控制器后保留选择,则需要将所选项目存储在

我知道在行选择中,您可以使用以下代码显示复选标记:

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
如果用户在不同的视图控制器之间移动太多,如何使复选标记保留在选定的行上


感谢您将
UITableViewController
设置为
NO

如果要在解除分配表视图控制器后保留选择,则需要将所选项目存储在类似
NSUserDefaults
的位置

-(void)viewDidLoad
{    
    [super viewDidLoad]; 
    NSArray *previousSelection = [[NSUserDefaults standardDefaults] objectForKey:@"selection"];
    for (NSArray *selection in previousSelection) {
         [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:selection[1] inSection:selection[0]] animated:NO scrollPosition:UITableViewScrollPositionNone];
    }
}

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    NSArray *selections = [self.tableView indexPathsForSelectedRows];
    NSMutableArray *selectionsToSave = [NSMutableArray array];
    for (NSIndexPath *selection in selections) {
        [selectionsToSave addObject:@[selection.section, selection.row]];
    }
    [[NSUserDefaults standardDefaults] setObject:selectionsToSave forKey:@"selection"];
    // Save as iOS 7 now saves less frequently - see note
    [[NSUserDefaults standardDefaults] synchronize];
}
关于使用

编辑:
由于@rmaddy指出您只能将简单类型放入
NSUserDefaults

中,您必须存储特定单元格的索引路径,因此更正了答案。这不起作用。您不能将
nsindepath
对象存储在
NSUserDefaults
@rmaddy中,这应该更好!谢谢你发现:)这应该行得通,但就我个人而言,我永远不会这么做。最好为每个选定行存储某种类型的键,以防行的顺序或数量发生变化。是的,最好使用核心数据来支持数据,但OP没有提供太多信息。。。!
-(void)viewDidLoad
{    
    [super viewDidLoad]; 
    NSArray *previousSelection = [[NSUserDefaults standardDefaults] objectForKey:@"selection"];
    for (NSArray *selection in previousSelection) {
         [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:selection[1] inSection:selection[0]] animated:NO scrollPosition:UITableViewScrollPositionNone];
    }
}

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    NSArray *selections = [self.tableView indexPathsForSelectedRows];
    NSMutableArray *selectionsToSave = [NSMutableArray array];
    for (NSIndexPath *selection in selections) {
        [selectionsToSave addObject:@[selection.section, selection.row]];
    }
    [[NSUserDefaults standardDefaults] setObject:selectionsToSave forKey:@"selection"];
    // Save as iOS 7 now saves less frequently - see note
    [[NSUserDefaults standardDefaults] synchronize];
}