Iphone:在 list 中保存复选标记位置

标签 iphone objective-c ios uitableview nsuserdefaults

尝试在我的应用中实现设置。我需要的是一份 list 。我用过

- (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 时恢复它...该怎么做?谢谢...

最佳答案

您可以将单元格文本的值保存到您的 NSUserDefaults,然后在 cellForRowAtIndexPath:

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

关于Iphone:在 list 中保存复选标记位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6075349/

相关文章:

ios - 我如何使用 Tapku Library 在特定日期放置 Day Event?

iphone - MFMailComposeViewController 电子邮件地址无效?

ios - 从 iPhone 上的 Exchange IMAP 邮箱中删除邮件

iphone - iOS 的 WebDav 客户端库

ios - Cocoapods 测试链接器错误

iphone - html5 本地应用程序可以有 asp.net session 吗? (适用于 iPhone 的本地网络应用程序)

iOS 从 .plist 文件加载数据

iphone - 如何在不使用 segues 和 NaviagationController 的情况下在两个 ViewController 之间传递数据

ios - 是否可以使用 iOS Facebook SDK 检索用户的_whole_好友列表?

iphone - iPhone 上的密码文本框