Iphone: сохранить позицию чекмарка в контрольном списке - PullRequest
1 голос
/ 20 мая 2011

Попытка реализовать настройки в моем приложении. Что мне нужно, это контрольный список. Я использовал

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath  *)indexPath {
     static NSString *CheckMarkCellIdentifier = @"CheckMarkCellIdentifier";
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CheckMarkCellIdentifier];
     if (cell == nil) { 
         cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CheckMarkCellIdentifier] autorelease];
     } 
     NSUInteger row = [indexPath row];
     NSUInteger oldRow = [lastIndexPath row]; 
     cell.textLabel.text = [list objectAtIndex:row]; 
     cell.accessoryType = (row == oldRow && lastIndexPath != nil) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone; 
     return cell;
 }

и

- (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; 
         lastIndexPath = indexPath;
     } 
     [tableView deselectRowAtIndexPath:indexPath animated:YES];
     [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:lastIndexPath.row] forKey:@"lastIndexPath"];
     [[NSUserDefaults standardUserDefaults] synchronize];
 }

Мне нужно сохранить положение checkMark в UserDefaults ... И восстановить его, когда я снова открою Check List ... Как это сделать? Спасибо ...

1 Ответ

2 голосов
/ 20 мая 2011

Что вы можете сделать, это сохранить значение текста ячейки в NSUserDefaults, а затем сравнить его в cellForRowAtIndexPath:

if([cell.textLabel.text isEqualToString:[[NSUserDefault standardUserDefaults] objectForKey:@"someValue"]]){
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else{
    cell.accessoryType = UITableViewCellAccessoryNone;
}
...