-3

Alright so I've two ViewController. One is the root and the other one is a filterView that applies some filters on root view controller. FilterView has a TableView on it with multiple selection options. When I tap the UIButton on root view, it shows the filter view which has the table and I select multiple options form it and when I dismiss the filterView it successfully dismiss it and by using a delegate method it successfully made changes to the RootViewController.
See the image below to have a more clear picture. The button on left (Three Horizontal Bars) will show the filter view controller and similarly the down button shows root view again. Image defines the two views I have
My question is that when I again taps on the button it load the whole table all over again. With no selected value. I want it to hold the checked values so that user can un-check them and check new filter values.

When user taps the filter button here is the code which I'm using:

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

And for back button on FilterView I'm using this:

[self dismissViewControllerAnimated:YES completion:nil];

So what should i write in back button so that it should keep the multiple checked rows and show if the user view it again. Or something which hide it in the background and show when needed.

Chaudhry Talha
  • 7,231
  • 11
  • 67
  • 116

4 Answers4

1

First Set an NSMutableArray as

    NSMutableArray *SelectedRows;

Now on Your View Did Load Set NSUserDefault as

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

And your TableView cellForRowAtIndexPath method write like as

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;

And Your didSelectRowAtIndexPath look like as

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];

Hope it Help You.

iOS Developer
  • 133
  • 3
  • 14
0

When you are pressing your filter button, you are creating a completely new FilterViewController, that's why it won't have any selection or anything from the previous one. If you want to have persistence, you'll need either to always present the same one (so doing instantiateViewControllerWithIdentifier once and keeping it around in a property, or after creating it and before presenting it, passing some data to it so it can restore it's status (see Passing Data between View Controllers)

Community
  • 1
  • 1
Fabio Ritrovato
  • 2,546
  • 1
  • 13
  • 19
-1

You can persist the selected rows using NSUserDefaults, create a array of indexPaths and when you select/deselect the tableview cell, add/remove the indexPath to/from the array. When you load the tableView just check if the indexPath is present in the array then keep it selected.

- (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];
}
Suhit Patil
  • 11,748
  • 3
  • 50
  • 60
-1

STEP 1:
In your .h file assign a variable FilterViewController *fvc;
STEP 2:
In your .m file write this line in ViewDidLoad Method.
fvc = [self.storyboard instantiateViewControllerWithIdentifier:@"FilterView"];
STEP 3:
Copy/Paste the code below as your - (IBAction)filterButton:(id)sender-

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

That's it!