【问题标题】:UITableViewController should not lose value on dismissing its ViewControllerUITableViewController 不应该在解除其 ViewController 时失去价值
【发布时间】:2015-01-02 11:53:48
【问题描述】:

好的,我有两个ViewController。一个是根,另一个是在根视图控制器上应用一些过滤器的 filterView。 FilterView 上有一个TableView,它有多个选择选项。当我在根视图上点击UIButton 时,它显示了具有表格的过滤器视图,我从它中选择了多个选项,当我关闭 filterView 时,它成功地关闭了它,并通过使用委托方法成功地对 @ 进行了更改987654325@。
请参阅下图以获得更清晰的图片。左侧的按钮(三个水平条)将显示过滤视图控制器,类似地,向下按钮再次显示根视图。
我的问题是,当我再次点击按钮时,它会重新加载整个表格。没有选定的值。我希望它保留选中的值,以便用户可以取消选中它们并检查新的过滤器值。

当用户点击过滤器按钮时,这是我正在使用的代码:

- (IBAction)filterButton:(id)sender
{
    FilterViewController *arvc = [self.storyboard instantiateViewControllerWithIdentifier:@"FilterView"];
    [self presentViewController:arvc animated:YES completion:nil];
}

对于 FilterView 上的后退按钮,我使用的是这个:

[self dismissViewControllerAnimated:YES completion:nil];

那么我应该在后退按钮中写什么,以便它应该保留多个选中的行并显示用户是否再次查看它。或将其隐藏在背景中并在需要时显示的东西。

【问题讨论】:

  • 设置 TableviewCell 附件类型在您的 TableView 中已选中。并将其存储在 NSUserDefault 中。
  • @iOSDeveloper 怎么做?你能提供一个示例代码吗?谢谢
  • 我为它提供了一个代码。
  • 查看我的更新答案。
  • 为什么对答案投反对票,请评论?

标签: ios objective-c uitableview uiviewcontroller


【解决方案1】:

首先将 NSMutableArray 设置为

    NSMutableArray *SelectedRows;

现在在您的视图中是否将 NSUserDefault 加载为

 NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
SelectedRows = [NSMutableArray arrayWithArray:[userDef objectForKey:@"SelectedRows"]];

你的 TableView cellForRowAtIndexPath 方法写成这样

static NSString *CellIdentifier=@"Cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
   //yourCode
}
NSNumber *obj = [NSNumber numberWithInteger:indexPath.row];
if ([SelectedRows containsObject:obj])
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
    cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;

你的didSelectRowAtIndexPath 看起来像

NSNumber *obj = [NSNumber numberWithInteger:indexPath.section];
if ([SelectedRows containsObject:obj])
{
    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
    [SelectedRows removeObject:obj];
    [tableView reloadData];
}
else
{
    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
    [SelectedRows addObject:obj];
    [tableView reloadData];
}

NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
[userDef setObject:SelectedRows forKey:@"SelectedRows"];
[userDef synchronize];

希望对您有所帮助。

【讨论】:

  • @FabioRitrovato 然后将您的解决方案放置在 NSUserDefault。
【解决方案2】:

当您按下过滤器按钮时,您正在创建一个全新的 FilterViewController,这就是为什么它不会有任何选择或前一个中的任何内容。如果你想拥有持久性,你需要要么总​​是呈现同一个(这样做一次 instantiateViewControllerWithIdentifier 并将其保存在一个属性中,或者在创建它之后并在呈现它之前,传递一些数据到它,以便它可以恢复它的状态(见Passing Data between View Controllers

【讨论】:

    【解决方案3】:

    第 1 步:
    在您的 .h 文件中分配一个变量 FilterViewController *fvc;
    第 2 步:
    在您的.m 文件中,在ViewDidLoad 方法中写入这一行。
    fvc = [self.storyboard instantiateViewControllerWithIdentifier:@"FilterView"];
    第 3 步:
    复制/粘贴以下代码作为您的 - (IBAction)filterButton:(id)sender-

    (IBAction)filterButton:(id)sender
    {
        [self presentViewController:fvc animated:YES completion:nil];
    }
    

    就是这样!

    【讨论】:

      【解决方案4】:

      您可以使用 NSUserDefaults 持久化选定的行,创建一个 indexPaths 数组,当您选择/取消选择 tableview 单元格时,将 indexPath 添加到/从数组中删除。当您加载 tableView 时,只需检查 indexPath 是否存在于数组中,然后保持选中状态。

      - (void)viewDidLoad {
          [super viewDidLoad];
          // Do any additional setup after loading the view.
      
          _indexPathArray = [[NSMutableArray alloc] init];
         _indexPathArray  =  [[NSUserDefaults standardUserDefaults] objectForKey:@"selectedRows"];
      
      }
      
      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      
          UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
          cell.textLabel.text = [NSString stringWithFormat:@"Row %ld", (long)indexPath.row];
      
          if ([_indexPathArray containsObject:indexPath]) {
              cell.accessoryType = UITableViewCellAccessoryCheckmark;
          } else {
              cell.accessoryType = UITableViewCellAccessoryNone;
          }
      
          return cell;
      
      }
      
      - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
      
          if ([_indexPathArray containsObject:indexPath]) {
              [_indexPathArray removeObject:indexPath];
          }
          else {
              [_indexPathArray addObject:indexPath];
          }
      
          [[NSUserDefaults standardUserDefaults] setObject:_indexPathArray forKey:@"selectedRows"];
          [[NSUserDefaults standardUserDefaults] synchronize];
          [self.tableView reloadData];
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-08-19
        • 2023-03-22
        • 2017-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-10
        相关资源
        最近更新 更多