【发布时间】:2014-12-09 21:36:33
【问题描述】:
在previous SO question 之后,我试图通过保存和其他内容正确选择 tableView 单元格。
我已将其切换,但无法保存 indexPath 并检索它以显示选择,即使用户离开视图或重新启动应用程序也是如此。
代码如下:
@property (nonatomic, strong) NSIndexPath* lastIndexPath;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
if([self.lastIndexPath isEqual:indexPath])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
...
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if(self.lastIndexPath)
{
UITableViewCell* uncheckCell = [tableView
cellForRowAtIndexPath:self.lastIndexPath];
uncheckCell.accessoryType = UITableViewCellAccessoryNone;
}
if([self.lastIndexPath isEqual:indexPath])
{
self.lastIndexPath = nil;
}
else
{
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
self.lastIndexPath = indexPath;
}
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSNumber numberWithInt:self.lastIndexPath.row] forKey:@"selectedCell"];
[defaults synchronize];
}
即使在用户关闭并重新打开视图后,如何保存 indexPath 并检索它以显示选择?
提前致谢。
【问题讨论】:
-
如果只需要单选,则不需要
selArray。只需跟踪选定的索引路径。 -
我试过了,但我无法保存 indexPath 并检索检查状态。
标签: ios objective-c uitableview