UITableView 复选标记仅选择一项
我使用下面的代码将 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"];
}
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];
}
}
[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];
*/
}
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"];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认情况下,NSUserDefaults 在应用程序从内存中卸载之前保存。我遇到的一个问题是,如果您从 Xcode 退出应用程序,则永远不会调用各种清理方法。将对象保存为默认值后,将
[[NSUserDefaults standardUserDefaults]一线同步];
添加到您的代码中。这应该会强制 NSUserDefaults 保存当时所有未保存的更改。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.