Iphone 使用NSUserDefults在UITableView上持久化检查列表

Iphone 使用NSUserDefults在UITableView上持久化检查列表,iphone,objective-c,xcode,Iphone,Objective C,Xcode,我有一个非常简单的表视图,它显示了一个日期列表。一旦用户选择与其相关的日期,该数据将保存在NSUserdefaults中。然后,我需要在用户退出后保留复选标记,然后重新进入表视图 我很快就可以获得所需的功能了-我可以保存复选标记项的数组,并使用NSUserDefaults使其保持不变,但我不知道如何使这些选择保持不变(在所选项旁边保留复选标记),一旦用户退出,然后重新进入表视图 我知道我需要编辑cellForRowAtIndexPath方法,但我不确定具体要做什么。任何帮助都将不胜感激 我已在下

我有一个非常简单的表视图,它显示了一个日期列表。一旦用户选择与其相关的日期,该数据将保存在NSUserdefaults中。然后,我需要在用户退出后保留复选标记,然后重新进入表视图

我很快就可以获得所需的功能了-我可以保存复选标记项的数组,并使用NSUserDefaults使其保持不变,但我不知道如何使这些选择保持不变(在所选项旁边保留复选标记),一旦用户退出,然后重新进入表视图

我知道我需要编辑cellForRowAtIndexPath方法,但我不确定具体要做什么。任何帮助都将不胜感激

我已在下面附上我的代码:

#import "DayView.h"

@implementation DayView

@synthesize sourceArray;
@synthesize selectedArray;



- (id)init
{
    self = [super initWithStyle:UITableViewStyleGrouped];
    if (self) {
    // Custom initialization
    [[self navigationItem] setTitle:@"Days"];

    [[self tableView] setBackgroundColor:[UIColor clearColor]]; 

}
return self;
}

- (void)viewWillDisappear:(BOOL)animated
{
// create a standardUserDefaults variable
NSUserDefaults * standardUserDefaults = [NSUserDefaults standardUserDefaults];

// Convert array to string
NSString *time = [[selectedArray valueForKey:@"description"] componentsJoinedByString:@","];

// saving an NSString
[standardUserDefaults setObject:time forKey:@"string"];

NSLog(@"Disapear: %@", time);
}



- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad {
[super viewDidLoad];

// create a standardUserDefaults variable
NSUserDefaults * standardUserDefaults = [NSUserDefaults standardUserDefaults];

// getting an NSString object
NSString *myString = [standardUserDefaults stringForKey:@"string"];

NSLog(@"Appear: %@", myString);


NSMutableArray * tempArray = [[NSMutableArray alloc] init];
[self setSelectedArray:tempArray];
[tempArray release];

NSArray * tempSource = [[NSArray alloc] initWithObjects:@"Monday", @"Tuesday", @"Wednesday", @"Thursday", @"Friday", @"Saturday",nil];
[self setSourceArray:tempSource];
[tempSource release];

[self.tableView reloadData];
}

#pragma mark -
#pragma mark Table view data source

// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return [self.sourceArray count];;
}


// Customize the appearance of table view cells.
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath   *)indexPath {

    static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}


NSString *time = [sourceArray objectAtIndex:indexPath.row];
cell.textLabel.text = time;


if ([self.selectedArray containsObject:[self.sourceArray objectAtIndex:indexPath.row]])

{
    [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
else
{
    [cell setAccessoryType:UITableViewCellAccessoryNone];
}


NSLog(@"Selected Days: %@", selectedArray);


return cell; 
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return @"Which times you are available?"; 
}

#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

if ([self.selectedArray containsObject:[self.sourceArray objectAtIndex:indexPath.row]]){
    [self.selectedArray removeObjectAtIndex:[self.selectedArray indexOfObject:   [self.sourceArray objectAtIndex:indexPath.row]]];
}
else
{
    [self.selectedArray addObject:[self.sourceArray objectAtIndex:indexPath.row]];
}

[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


@end

按如下方式创建tempSource:

NSMutableArray * tempSource = [[NSMutableArray alloc] init];
NSArray *daysOfWeek = [NSArray arrayWithObjects:@"Monday", @"tuestay", @"wednesday", @"thursday", @"friday", @"saturday", @"sunday",nil];
    for (int i = 0; i < 7; i++)
    {
        NSString *dayOfWeek = [daysOfWeek objectAtIndex:i];
        NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:dayOfWeek, @"day",   [NSNumber numberWithBool:NO], @"isSelected",nil];
       [tempSource addObject:dict];
    }

[self setSourceArray:tempSource];
[tempSource release];
didSelect

NSDictionary *dayOfWeekDictionary = [sourceArray objectAtIntex:indexPath.row];

if ([[dayOfWeekDictionary objectForKey:@"isSelected"] boolValue])
    [dayOfWeekDictionary setObject:[NSNumber numberWithBool:NO] forKey:@"isSelected"];
else
    [dayOfWeekDictionary setObject:[NSNumber numberWithBool:YES] forKey:@"isSelected"];
要保存此数组,请使用以下语句:

[[NSUserDefaults standardUserDefaults] setObject:sourceArray forKey:@"array"];

感谢您的快速响应,我尝试过实现您的解决方案,但到目前为止运气不佳,请您具体说明CellForRow和didSelect方法在使用您的代码后将如何处理。谢谢。谢谢你说得更清楚一点,我真的很感谢你的帮助。很抱歉,这是一个麻烦,但did select方法不起作用-它显示了两个警告,说明NSDictionary可能不会响应setObjectForKey。你知道这可能是什么吗?很抱歉再次打扰你-我已经做了你建议的更改,但是当选择了一行时它会崩溃(日志中没有显示错误)。你是否将添加到tempSource NSMutaleDictionary*dict=[NSMutaleDictionary Dictionary WithObjectsAndKeys:dayOfWeek,@“day”,[NSNumber numberWithBool:No],@“isSelected”,无];[tempSource addObject:dict];感谢Vladislav,它解决了这个问题-现在唯一的问题是它在返回视图时没有保存选中的项。我尝试添加您编写的代码,并将其添加到ViewWildisaper中,但没有成功。
[[NSUserDefaults standardUserDefaults] setObject:sourceArray forKey:@"array"];