开发者

Iphone: save chekmark position in Check List

开发者 https://www.devze.com 2023-03-07 03:11 出处:网络
Trying to implement settings in my app. What I need is a check list. I used - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {

Trying to implement settings in my app. What I need is a check list. I used

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath  *)indexPath {
     static NSString *CheckMarkCellIdentifier = @"CheckMarkCellIdentifier";
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CheckMarkCellIdentifier];
     if (cell == nil) { 
         cell = [[[UITableViewCell alloc]initWithStyle:UITab开发者_Python百科leViewCellStyleDefault 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;
 }

and

- (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];
 }

What I need is to save checkMark position to UserDefaults...And restore it when I will open Check List again...How to do that ? Thanks...


What you can do is save the value of the cell text to your NSUserDefaults then compare it in cellForRowAtIndexPath:

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

精彩评论

暂无评论...
验证码 换一张
取 消