如何在重新加载视图后保存tableView单元格的复选标记使用NSUserDefaults?

我是新来的iOS开发,我使用复选标记的UITableView单元格。

我想存储在NSUserDefaults数据库中检查过的单元格,当我重新加载应用程序时,选中的单元格将显示,我尝试不同的方式,但仍然无法实现它。

任何人都可以帮助我? 我将不胜感激。 对不起,我的英文不好。

我的代码如下:

 #import "FirstRowSubview.h" @interface FirstRowSubview () @end @implementation FirstRowSubview @synthesize array = _array; @synthesize lastIndexPath = _lastIndexPath; - (void)viewDidLoad { [super viewDidLoad]; NSMutableArray *list = [[NSMutableArray alloc] initWithObjects:@"ffgfgh", @"564654", @"56548", @"fgmjfgmf", @"ggkdj", nil]; self.array = list; [list release]; } - (void)viewDidUnload { [super viewDidUnload]; self.array = nil; } #pragma mark - #pragma mark - TableView Datasource Methods - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [_array count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *subviewCells = @"Cells"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:subviewCells]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:subviewCells]; } NSUInteger oldRow = [_lastIndexPath row]; cell.accessoryType = (indexPath.row == oldRow && _lastIndexPath != nil) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone; cell.textLabel.text = [_array objectAtIndex:indexPath.row]; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [defaults setObject:[NSNumber numberWithInt:_lastIndexPath.row] forKey:@"lastIndexPath"]; [defaults synchronize]; return cell; } #pragma mark - #pragma mark - TableView Delegate Methods - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { int newRow = [indexPath row]; int oldRow = (_lastIndexPath != nil) ? [_lastIndexPath row] : -1; if (newRow != oldRow) { UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath]; newCell.accessoryType = UITableViewCellAccessoryCheckmark; UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:_lastIndexPath]; oldCell.accessoryType = UITableViewCellAccessoryNone; [indexPath retain]; [_lastIndexPath release]; _lastIndexPath = indexPath; [tableView deselectRowAtIndexPath:indexPath animated:YES]; } } 

使用NSUserDefaults可以很容易地存储和检索选中的单元格信息。

NSUserDefaults可以存储键/值对的数据。 在你的情况下,值可以是布尔值,键可以组合UITableViewNSStringindexpath.row

你可以做一个function,

 - (NSString *)getKeyForIndex:(int)index { return [NSString stringWithFormat:@"KEY%d",index]; } - (BOOL) getCheckedForIndex:(int)index { if([[[NSUserDefaults standardUserDefaults] valueForKey:[self getKeyForIndex:index]] boolValue]==YES) { return YES; } else { return NO; } } - (void) checkedCellAtIndex:(int)index { BOOL boolChecked = [self getCheckedForIndex:index]; [[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithBool:!boolChecked] forKey:[self getKeyForIndex:index]]; [[NSUserDefaults standardUserDefaults] synchronize]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [_array count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //Here, you can check for previously checked cells like static NSString *subviewCells = @"Cells"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:subviewCells]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:subviewCells]; } cell.textLabel.text = [_array objectAtIndex:indexPath.row]; if([self getCheckedForIndex:indexPath.row]==YES) { cell.accessoryType = UITableViewCellAccessoryCheckmark; } else { cell.accessoryType = UITableViewCellAccessoryNone; } return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:NO]; //Use checkedCellAtIndex for check or uncheck cell UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; [self checkedCellAtIndex:indexPath.row]; if([self getCheckedForIndex:indexPath.row]==YES) { cell.accessoryType = UITableViewCellAccessoryCheckmark; } else { cell.accessoryType = UITableViewCellAccessoryNone; } } 

PS请注意,这种方法只会有用,并build议如果您的NSArray数据的顺序将始终以相同的顺序。 其他然后NSUserDefaults你有select像SQLite Databaseplist file或任何其他选项! 谢谢 :)

这是如何基于当前的表select创build一个NSIndexPaths数组,然后将该数组保存到NSUserDefaults

  -(void)viewDidLoad { [super viewDidLoad]; for (NSIndexPath *indexPath in [[NSUserDefaults standardUserDefaults] mutableArrayValueForKey:@"mySavedMutableArray"]) { [self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone]; } } 

在这里你可以添加对象到数组中,因为它们被选中

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [[[NSUserDefaults standardUserDefaults] mutableArrayValueForKey:@"mySavedMutableArray"] addObject:indexPath]; [[NSUserDefaults standardUserDefaults] synchronize]; } 

在这里,您将从数组中删除取消select的对象。

 - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { [[[NSUserDefaults standardUserDefaults] mutableArrayValueForKey:@"mySavedMutableArray"] removeObject:indexPath]; [[NSUserDefaults standardUserDefaults] synchronize]; } 

让代码工作的最简单的方法就是在视图中join一行代码,如下所示:

 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; if([defaults valueForKey:@"lastIndexPath"]) _lastIndexPath = [defaults valueForKey:@"lastIndexPath"]; 

这将解决您的问题:)

快乐编码:)