UITableView Checkmark Only Select One

时间:2011-02-27 16:31:27

标签: iphone objective-c xcode uitableview checkmark

我使用下面的代码将UITableViewCell的附件设置为复选标记,然后写入NSUserDefaults。但它似乎没有正常工作。似乎永远不会设置NSUserDefault。如果您能看到问题,请指出。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *text = [array objectAtIndex:indexPath.row];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
        if ([text isEqualToString:[NSString stringWithFormat:@"%i", k10]]) {
            [defaults setInteger:k10 forKey:kNumberOfChartsToDownload];
        } else if ([text isEqualToString:[NSString stringWithFormat:@"%i", k25]]) {
            [defaults setInteger:k25 forKey:kNumberOfChartsToDownload];
        } else if ([text isEqualToString:[NSString stringWithFormat:@"%i", k50]]) {
            [defaults setInteger:k50 forKey:kNumberOfChartsToDownload];
        } else if ([text isEqualToString:[NSString stringWithFormat:@"%i", k75]]) {
            [defaults setInteger:k75 forKey:kNumberOfChartsToDownload];
        } else if ([text isEqualToString:[NSString stringWithFormat:@"%i", k100]]) {
            [defaults setInteger:k100 forKey:kNumberOfChartsToDownload];
        }
    }
    [self.navigationController popViewControllerAnimated:YES];
} 
else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
    cell.accessoryType = UITableViewCellAccessoryNone;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];

// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
 // ...
 // Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
*/
}

当应用程序启动时,我也是NSLogging NSUserDefault。但是整数是100。如果你能看到问题,请这样做。

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (! [defaults objectForKey:@"firstRun"]) {
    [defaults setObject:[NSDate date] forKey:@"firstRun"];
}
NSInteger daysSinceInstall = [[NSDate date] timeIntervalSinceDate:[defaults objectForKey:@"firstRun"]] / 86400;
if (! [defaults objectForKey:@"first"]) {
    [defaults setObject:[NSDate date] forKey:@"first"];
}
if (daysSinceInstall == 0 && [defaults boolForKey:@"askedForSettings"] == NO) {
    if (![defaults integerForKey:kNumberOfChartsToDownload]) {
        NSLog(@"no defaults");
        [defaults setInteger:k25 forKey:kNumberOfChartsToDownload];
    } else if ([defaults integerForKey:kNumberOfChartsToDownload]) {
        NSLog(@"%i", [defaults integerForKey:kNumberOfChartsToDownload]);
    }
    [defaults setBool:YES forKey:@"askedForSettings"];
} 

1 个答案:

答案 0 :(得分:2)

默认情况下,NSUserDefaults会在从内存中卸载应用程序之前保存。我遇到的一个问题是,如果从Xcode退出应用程序,则永远不会调用各种清理方法。完成将对象保存为默认值后,将[[NSUserDefaults standardUserDefaults] synchronize];添加到代码中。这应该强制NSUserDefaults保存当时未保存的任何更改。