【问题标题】:How to Save Selected TableView Row Data and Show when user Come Back to Screen?如何保存选定的 TableView 行数据并在用户返回屏幕时显示?
【发布时间】:2014-12-29 06:25:52
【问题描述】:

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

我在 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;

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

【问题讨论】:

  • 你能显示你的 cellForRowAtIndexPath 方法的代码吗?您需要使用 NSUSerDefaults 生成单元格视图。您还在使用 indexPath.section 而不是 indexPath.row 吗?您的表格视图中有多个部分吗?
  • @NewStackUser 我在这里写 cellForRowAtIndexPath 代码,在这里我的 Tableview 多个部分。
  • @iOSDeveloper- 查看我的答案,您可以用您的 nsuserdefault 值替换我的属性。
  • @All 感谢您帮助我拯救我的一天。

标签: ios objective-c iphone uitableview


【解决方案1】:

创建如下所示的属性

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

@end

检查它是否为 nil 或值是否存在于 indexpath 中

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

【讨论】:

  • 对我很有帮助...!
  • 太棒了!惊人的!谢谢!
【解决方案2】:

试试这个。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多