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

时间:2014-12-29 06:25:53

标签: ios objective-c iphone uitableview

我是iOS开发中的新手。我想制作一个包含UITableview的应用程序。我想显示当用户选择UITableView Cell然后它像检查标记它是按我想要的工作,但现在我想显示当用户回到其他视图并回到表视图然后它显示您过去选择表视图单元格请给我解决方案。

我在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中的获取NSUserdefault数据,如

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

和我在我的.h文件中定义

@property (nonatomic,retain) NSIndexPath *lastIndexPath;

但我没有获得用户选择回到视图请给我解决方案。

2 个答案:

答案 0 :(得分:3)

创建如下所示的属性

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

@end

检查indexpath中是否存在nil或value

- (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;
}

答案 1 :(得分:-1)

试试这个。

-(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];
if (indexPath.section==self.lastIndexPath) {
    cell.accessoryType=UITableViewCellAccessoryCheckmark;
}
else{
    cell.accessoryType=UITableViewCellAccessoryNone;
}
return cell;
}