Save accessory checkmarks on uitableview when users loads view multiple times

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 05:51:48

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.

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;
}
Ajay Singh Thakur
- (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;


}

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.

The best way to save tableView CheckMark with NSUserDefaults

    @interface TableViewController (){
    NSArray *TableTitles;
    NSMutableArray *SelectedRows;
}

viewDidLoad

   - (void)viewDidLoad
{
    [super viewDidLoad];

    // table view array
    TableTitles = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10", nil];

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

cellForRowAtIndexPath

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    cell.textLabel.text = TableTitles[indexPath.row];

    NSNumber *obj = [NSNumber numberWithInteger:indexPath.row];
    if ([SelectedRows containsObject:obj])
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}

didSelectRowAtIndexPath

 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSNumber *obj = [NSNumber numberWithInteger:indexPath.row];
    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];
}

updated ...

i updated all code with a new way , it's the best way to save TableView CheckMarks and it's now easy and efficient, now only one array is used for saving and loading , code reduced too much :)

You should load data in NSUserDefaults while call cellForRowAtIndexPath

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

    //You should set accessoryType here by NSUserDefaults data
    if ([self getCheckedForIndex:indexPath.row]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }


    return cell;
}

I think what you are looking for is to get the current state of the table for the filters when you enter and exit the view. You need to utilize the - (void)viewWillDisappear:(BOOL)animated to save the current state of the table to your NSUserDefaults, and then use - (void)viewDidLoad and - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath to set the checkmark when the table reloads.

@interface FiltersViewController () {
    NSMutableArray *_filterStates;
}

@end

- (void)viewDidLoad {
    filterStates = [[NSMutableArray alloc] init];

    // get state of your current filters in NSUserDefaults, these should be stored as [NSNumber numberWithBool:]
}

- (void)viewWillDisappear:(BOOL)animated {
    // store all of your filters into your NSUserDefaults as [NSNumber numberWithBool:]
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // get your cell from the table

    BOOL isChecked = [[_filterStates objectAtIndex:indexPath.row] boolValue];

    // set the checkmark based on the current state for this filter
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!