ios - 如何保存选定的 TableView 行数据并在用户返回屏幕时显示?

标签 ios objective-c iphone uitableview

我是 iOS 开发新手。我想制作一个包含 UITableview 的应用程序。我想在用户选择 UITableView 单元格时显示,然后它就像勾选标记一样按我想要的方式工作,但现在我想显示当用户返回其他 View 并返回 TableView 时,它会显示你过去选择的 TableView 单元格请给我解决方案。

我在 Stack Overflow 中显示它是由 NSUSerdefault 完成的我找到了它的代码并设置为但它对我不起作用请给我解决方案。

这是我的 UITableview didSelectRowAtIndexPath 方法。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CategoryCustumCell *cell = (CategoryCustumCell *)[tableView cellForRowAtIndexPath:indexPath];
NSString *selectedCell= cell.categoryLabel.text;
NSLog(@"Category label %@",selectedCell);
NSString *text=[self.categoryArray objectAtIndex:indexPath.section];
if([self isRowSelectedOnTableView:tableView atIndexPath:indexPath])
{
    [self.selectedCells removeObject:indexPath];
    [self.selecedStates removeObject:text];
    cell.accessoryType = UITableViewCellAccessoryNone;
} else
{
    [self.selectedCells addObject:indexPath];
    [self.selecedStates addObject:text];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
NSLog(@"%@", self.selecedStates);
NSUserDefaults *userdefaults = [NSUserDefaults standardUserDefaults];
[userdefaults setObject:[NSString stringWithFormat:@"%d",indexPath.section] forKey:@"lastIndexPathUsed"];
[userdefaults synchronize];
}

还有 UITableView 的代码 cellForRowAtIndexPath

-(CategoryCustumCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier=@"Cell";
CategoryCustumCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
    NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"CategoryCustumCell" owner:self options:nil];
    cell=[nib objectAtIndex:0];
}
cell.categoryLabel.text=[self.categoryArray objectAtIndex:indexPath.section];
cell.accessoryType = ([self isRowSelectedOnTableView:tableView atIndexPath:indexPath]) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
return cell;
}

然后我像这样在 viewDidLoad 中写入 Fetch NSUserdefault Data

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
self.lastIndexPath = [defaults objectForKey:@"lastIndexPathUsed"];

我在我的.h文件中定义

@property (nonatomic,retain) NSIndexPath *lastIndexPath;

但我没有得到关于用户返回 View 的选择请给我解决方案。

最佳答案

创建如下属性

@interface ViewController ()
//I added this property to keep track of the selected row
@property (strong,nonatomic) NSIndexPath *selectedPath;

@end

检查它是否为 nil 或值是否存在于索引路径中

- (void)viewWillAppear:(BOOL)animated
{
    //I added this if clause to select the row that was last selected
    if (self.selectedPath != nil) {
        [TABLENAME selectRowAtIndexPath:self.selectedPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    }

}

在didSelect中设置值

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// set here
    self.selectedPath = indexPath;
}

关于ios - 如何保存选定的 TableView 行数据并在用户返回屏幕时显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27684338/

相关文章:

ios - 如何在 iOS sdk 中的现有组上添加联系人

ios - 如何使用括号内的字符串

ios - iOS 5 HTTP查询与JSON对象

objective-c - Objective C 结构语法

ios - 在对象UiTableViewCell上找不到属性

ios - 检查 SKAction 是否正在运行

iphone - 使用 xcodebuild 构建等待 <IDEWorkspace, 0x2004cebc0>/"runContextManager.runContexts"超时

ios - 绘图后如何分隔圆点

objective-c - Objective C block 作为异步回调和 BAD ACCESS

ios - NSPredicate 检查以逗号分隔的一串数字是否包含数字