ios表视图之UITableViewCellAccessoryCheckmark单选、多选状态保存

表视图界面跳转,单元格选中状态的保存

多选

在cellForRow中: 

        UITableViewCell *Cell = [tableView cellForRowAtIndexPath:indexPath];

Cell.accessoryType = [[NSUserDefault standardUserDefaults]objectForKey:indexPath]?UITableViewCellAccessoryCheckmark:UITableViewCellAccessoryNone; 

在didselecte这个方法中 

Cell.accessoryType = UITableViewCellAccessoryCheckmark; 

下面加一句 

[[NSUserDefault standardUserDefaults]setObject:[NSNumber numberWithBool:YES] forKey:indexPath];

单选

在cellForRow中: 

NSInteger row = [indexPath row];

    

    NSInteger oldRow = [NSUD integerForKey:@"indexPath.row"];

    

    NSLog(@"选中行业%ld",(long)oldRow);

    

    if (row == oldRow) {

        

        cell.accessoryType = UITableViewCellAccessoryCheckmark;

        

    }else{

        

        cell.accessoryType = UITableViewCellAccessoryNone;

        

    }

在didselecte这个方法中 

 NSInteger newRow = [indexPath row];

    

    NSInteger oldRow = ([NSUD initWithUser:@"indexPath.row"] !=nil)?[NSUD integerForKey:@"indexPath.row"]:-1;

    

    if (newRow != oldRow) {

        

        UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];

        

        newCell.accessoryType = UITableViewCellAccessoryCheckmark;

        

        UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:_lastPath];

        

        oldCell.accessoryType = UITableViewCellAccessoryNone;

        

        _lastPath = indexPath;

        

    }

    

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    

    [NSUD setInteger:_lastPath.row forKey:@"indexPath.row"];

原文地址:https://www.cnblogs.com/liuxiaokun/p/5530260.html