【发布时间】:2014-03-31 21:55:12
【问题描述】:
关于这个主题有很多问题,我已经用各种选项尝试了几个小时,但我需要一些真正的帮助。
主要前提是:我有一个Tab Bar controller,有两个标签;第一个是Timeline,第二个是In-App Settings(都是table view controllers)。在In-App settings 中,当我选择“键盘”时,我被转到另一个表格视图,其中包含两个静态单元格:浅色和深色。
我想要实现的是:当我点击其中一个单元格时,它会在该cell 上放置一个checkmark accessory 视图。
使用NSUserDefaults,我想维护选定的单元格并将该单元格的附件类型设置为下次视图重新加载时的复选标记。
我遵循了这个问题的选择答案,而我的代码允许我选择浅色或深色单元格;它永远不会在重新加载之间保持这一点。
这是我的cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
self.selectedKeyboardCell = [[NSUserDefaults standardUserDefaults] objectForKey:@"Selected"];
NSLog(@"The selected keyboard is %@", self.selectedKeyboardCell);
if([self.checkedIndexPath isEqual:indexPath])
{
NSLog(@"Loaded");
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
NSLog(@"Not Loaded");
cell.accessoryType = UITableViewCellAccessoryNone;
/*if ([self.selectedKeyboardCell isEqualToString:@"Light"])
{
NSLog(@"P");
if (indexPath.row == 0)
{
NSLog(@"Q");
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
cell.accessoryType = UITableViewCellAccessoryNone;
}
else
{
NSLog(@"R");
if (indexPath.row == 1)
{
NSLog(@"Z");
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
cell.accessoryType = UITableViewCellAccessoryNone;
*/
}
我的didSelectRowAtIndexPath 是:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Uncheck the previous checked row
if(self.checkedIndexPath)
{
UITableViewCell* uncheckCell = [tableView
cellForRowAtIndexPath:self.checkedIndexPath];
uncheckCell.accessoryType = UITableViewCellAccessoryNone;
}
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
self.checkedIndexPath = indexPath;
self.selectedKeyboardCell = cell.textLabel.text;
[[NSUserDefaults standardUserDefaults] setObject:self.selectedKeyboardCell forKey:@"Selected"];
self.selectedKeyboardTheme = cell.textLabel.text;
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[[NSUserDefaults standardUserDefaults] setObject:self.selectedKeyboardTheme forKey:@"Keyboard"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
我的viewWillAppear 中有一个[self.keyboardTableView reloadData];。
我已经注释掉了一些我一直在尝试的代码,但是无论我阅读了多少帖子,我都无法在这里找到解决方案。
这个问题谈到将代码转移到 viewWillAppear 但我不知道他在说什么代码:Ho to refresh view and tableview, and checkmark a default setting cell on load?
问题:我想在表格视图中选择一个单元格,并在下次(以及每次加载此视图时)使用复选标记附件类型选择该单元格。这是一个静态表格,文本只会说 Light 或 Dark。
在cellForRowAtIndexPath 中,self.selectedKeyboardCell 准确地告诉我是浅色还是深色,这取决于在加载视图之前选择了哪个单元格。但是,我怎么说“如果亮,在第一个单元格上设置复选标记并从第二个单元格中删除复选标记,如果是暗,在第二个单元格上设置复选标记并从第一个单元格中删除复选标记”。
更新:
为清楚起见,我可以选择 Light 或 Dark 静态表格视图单元格,无论我选择哪个单元格,复选标记都会正确显示。问题是当我转到另一个视图并返回此视图时,先前选择的单元格不显示。我将 selectedCell 的 textLabel 保存到 NSUserDefaults 并在 NSLog 中,它显示了所选单元格的文本,但没有显示所选单元格上的复选标记。
更新二: 为了在应用程序第一次运行时将默认选定的单元格设置为 Light,我在 cellForRowAtIndexPath 中执行了以下操作:
if (!self.selectedKeyboardCell)
{
self.checkedIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
对此的任何指导将不胜感激。
【问题讨论】:
标签: ios iphone nsuserdefaults uitableview