2011-04-08 60 views
2

我正在申请有UItableView。在表格的行中,我可以放置复选标记。现在如何保存复选标记的状态,以便即使用户关闭应用程序 ,也应该删除状态并在下一次启动应用程序复选标记时显示。 我按照NSUSerDefaults上的教程,但拉我的头发放在哪里保存和检索的代码。我已经尝试过,但每次错误填满我,无法修复。 我的代码:如何保存在userdefaults的状态附件复选标记-iphone

MY.h文件

**@protocol LocationSelectionViewControllerDelegate <NSObject> 

@required 
- (void)rowSelected:(NSString *)selectedValue selectedIndex:(NSInteger)index; 
@end** 

@interface LocationSelection : UIViewController <UITableViewDelegate,UITableViewDataSource>{ 

UITableView *table; 
***NSInteger  selectedIndex;*** 
NSMutableArray *menuList; 
***id <LocationSelectionViewControllerDelegate> delegate;*** 

} 
@property (nonatomic,retain) NSMutableArray *menuList; 
@property (nonatomic,retain) IBOutlet UITableView *table; 
***@property (nonatomic, assign) id <LocationSelectionViewControllerDelegate> delegate;** 
**@property NSInteger selectedIndex;*** 
@end 

我.m文件:

@implementation LocationSelection 
***@synthesize menuList, table,selectedIndex,delegate;*** 

- (void)viewDidLoad 
{ 
    menuList = [[NSMutableArray alloc] initWithObjects: 
       [NSArray arrayWithObjects:@"LOCATION1", nil], 
       [NSArray arrayWithObjects:@"LOCATION2", nil], 
       [NSArray arrayWithObjects:@"LOCATION3", nil], 
       nil]; 

    self.title = @"Location Selection"; 

    [table reloadData]; 
    [super viewDidLoad]; 
} 

//MY CELLFORROWATINDEXPATH 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath 
{ 
    static NSString *CellIdentifier = @"CellIdentifier"; 

    UITableViewCell *cell = [table dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero] autorelease]; 
    } 
    cell.highlighted = NO; 

    ***NSArray * rowArray = [menuList objectAtIndex:indexPath.row];*** 

    UILabel * nameLabel = [[[UILabel alloc] initWithFrame:CGRectMake(15, 8, 200, 20)] autorelease]; 
    nameLabel.text = [NSString stringWithFormat:@"%@", [rowArray objectAtIndex:0]]; 
    [cell.contentView addSubview:nameLabel]; 

    ***cell.accessoryType = (rowArray == selectedIndex && selectedIndex > -1 && selectedIndex < [menuList count]) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone; 
    return cell; 
    *** 
} 

//MY DIDSELECTROWATINDEXH 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    int newRow = [indexPath row]; 

    if (newRow != selectedIndex) 
    { 
     UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath]; 
     newCell.accessoryType = UITableViewCellAccessoryCheckmark; 

     if (selectedIndex > - 1 && selectedIndex < [menuList count]) 
     { 
      NSUInteger newIndex[] = {0, selectedIndex}; 
      NSIndexPath *lastIndexPath = [[NSIndexPath alloc] initWithIndexes:newIndex length:2]; 
      UITableViewCell *oldCell = [tableView cellForRowAtIndexPath: lastIndexPath]; 
      oldCell.accessoryType = UITableViewCellAccessoryNone; 
     }  

     selectedIndex = newRow; 
     NSString *selectedValue=[menuList objectAtIndex:selectedIndex]; 
     [self.delegate rowSelected:selectedValue selectedIndex:selectedIndex]; 
    } 
} 
+0

请格式化您的代码,下次更好一点;) – 2011-04-08 07:01:48

+0

对不起!!感谢编辑,下次再来 – Alok 2011-04-08 07:03:26

回答

0

您好我认为它会适合你,如果你有一个对象(S)来表示对勾。您可以通过synchronize将其保存为用户默认值。在cellForRowATIndexPath:中,您可以检查该值是否存在于用户默认值中,如果是,则将该单元附件作为选中标记,如果不存在,则将其设为无。

1
- (void)applicationDidEnterBackground:(UIApplication *)application { 
} 

使用此方法来将数据保存到NSUserDefaults 而在LocationSelection

0

viewDidLoad设置数据放在一个状态在menuList反映细胞所以即使多选可以轻松完成的选择。您可以使用字典而不是数组。数组很好但不可读,所以你必须记住哪个字段包含什么并将其映射到索引。

当视图从你的userDefaults加载menuList数组时,当应用程序关闭时保存默认值。

didSelectRowAtIndexPath你保存选择在menuList

cellForRowAtIndexPath您阅读menuList和新的数组索引/字典密钥并设置复选标记。