Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save accessory checkmarks on uitableview when users loads view multiple times

So I've implemented a UIViewController with a tableview, and basically it loads as a set of "filters" for my uicollectionview.

Now, when I click on the checkmarks in my tableview, it "filters" my cells accordingly, but now when I reload the view again I want to display the most recent "checkmarks" I've used, or "filters."

I have seen this being implemented with NSUserDefaults, but I have not been able to successfully implement this.

If anyone could help me, that will be greatly appreciated.

CODE

FiltersViewController.m:

#import "FiltersViewController.h"

@interface FiltersViewController ()

@property (nonatomic, strong) NSMutableSet *selectedRowObjects;
//@property (nonatomic, strong) NSArray *filters;

@end

@implementation FiltersViewController


- (void)viewDidLoad
{
    [super viewDidLoad];

    self.selectedRowObjects = [NSMutableSet setWithCapacity:10];
}

- (IBAction)filtersSelected:(id)sender {
    [self.delegate filtersSelected:self.selectedRowObjects];
}

- (IBAction)cancelFilterSelection:(id)sender {
    [self.delegate filterSelectionCancelled];
}

- (NSString *)getKeyForIndex:(int)index
{
    return [NSString stringWithFormat:@"KEY%d",index];
}

- (BOOL) getCheckedForIndex:(int)index
{
    if([[[NSUserDefaults standardUserDefaults] valueForKey:[self getKeyForIndex:index]] boolValue]==YES)
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

- (void) checkedCellAtIndex:(int)index
{
    BOOL boolChecked = [self getCheckedForIndex:index];

    [[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithBool:!boolChecked] forKey:[self getKeyForIndex:index]];
    [[NSUserDefaults standardUserDefaults] synchronize];
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"filter" forIndexPath:indexPath];
    cell.textLabel.text = [NSString stringWithFormat:@"%u", indexPath.row];



    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSString *obj = cell.textLabel.text;

    if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
        cell.accessoryType = UITableViewCellAccessoryNone;
        [self.selectedRowObjects removeObject:obj];
    }
    else {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        [self.selectedRowObjects addObject:obj];
    }

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}
@end
like image 842
Varun Iyer Avatar asked Jul 26 '14 02:07

Varun Iyer


4 Answers

you need to check in cellForRowAtIndexPath also. Write this code in this

if([[NSUserDefaults standardUserDefaults] objectForKey:[self getKeyForIndex:indexPath.row]])
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
    cell.accessoryType = UITableViewCellAccessoryNone;
}

And yes don't forget to call this method in didSelectRowAtIndexPath

[self checkedCellAtIndex:indexPath.row];

Enjoy.

like image 122
Bhavesh Lathigara Avatar answered Oct 23 '22 20:10

Bhavesh Lathigara


You did almost everything right. You just need to put your logic for reading the NSUserDefault values (for the checked boxes) in the cellForRowAtIndexPath delegate method. That is the method that draws the UITableViewCells when they are displayed on screen. Something like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"filter" forIndexPath:indexPath];
    cell.textLabel.text = [NSString stringWithFormat:@"%u", indexPath.row];

    if ([self getCheckedForIndex:indexPath.row])
    {
        //code to set checkbox
    }

    return cell;
}
like image 34
Jordan Avatar answered Oct 23 '22 19:10

Jordan


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {

NSString *simpleTableIdentifier = @"cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = ArrayOfObject[indexPath.row];
NSUserDefaults *ud =[NSUserDefaults standardUserDefaults];
if([[ud objectForKey:@"selectedObjectKey "]isEqualToString:ArrayOfObject[indexPath.row]])
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
    cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;


}
like image 2
Ajay Singh Thakur Avatar answered Oct 23 '22 20:10

Ajay Singh Thakur


You have implemented perfectly. You just need to modify you cellForRowAtIndexPath Method

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"filter" forIndexPath:indexPath];
    cell.textLabel.text = [NSString stringWithFormat:@"%u", indexPath.row];

BOOL checked = [self getCheckedForIndex:indexPath.row];
   if(checked)
       cell.accessoryType = UITableViewCellAccessoryCheckmark;
   else
       cell.accessoryType = UITableViewCellAccessoryNone;


    return cell;
}

Also Call [self checkedCellAtIndex:indexPath.row]; from didSelectRowAtIndexPath method of UITableView.

like image 1
Surjeet Singh Avatar answered Oct 23 '22 19:10

Surjeet Singh