UITableView Checkmark Only Select One
I am using the code below to set a UITableViewCell's accessory to a checkmark and then write to NSUserDefaults. However it doesn't seem to be working properly. The NSUserDefault never seems to be set. If you can see the problem please point it out.
- (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];
}
}
[sel开发者_JS百科f.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];
*/
}
I am also NSLogging the NSUserDefault when the application launches. However the integer is allways 100. Please do the same if you can see the problem.
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"];
}
By default, NSUserDefaults saves just before the application is unloaded from memory. A problem I have encountered, is that if you quit your application from Xcode, various cleanup methods are never called. Add [[NSUserDefaults standardUserDefaults] synchronize];
to your code after you are done saving the objects in the defaults. This should force NSUserDefaults to save any unsaved changes at that time.
精彩评论