0

I am trying to save the checkmark that i place on a cell and then retrieve it. I have 11 cells in that section.

I tried to use this link as a guide and it works. But i want only need one checkmark to be saved rather than many.

And also i want to set checkmark for all cells by default. How would i achieve this

My Code :

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

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      static NSString *CellIdentifier = @"Cell";
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
      if (cell == nil) {
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

      if(section == 4) {
         if([self getCheckedForIndex:indexPath.row]==YES) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
          }
          else
          {
            cell.accessoryType = UITableViewCellAccessoryNone;
          }
      }
 }


 return cell;
 }

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
  UITableViewCell* cell = nil;

   NSString *key = [_keys objectAtIndex:indexPath.section];
   NSArray *name = [_names objectForKey:key];

   static NSString *MyCellIdentifier = @"Cell";

   cell = [tableView dequeueReusableCellWithIdentifier:nil];

   if (cell == nil){
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                  reuseIdentifier:MyCellIdentifier];

    cell.textLabel.text = [name objectAtIndex:indexPath.row];
    if(section == 4) {
   [self checkedCellAtIndex:indexPath.row];

   if([self getCheckedForIndex:indexPath.row]==YES)
   {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
   }
   else
   {
        cell.accessoryType = UITableViewCellAccessoryNone;
   }
    }

  [tableView deselectRowAtIndexPath:indexPath animated:YES];
 }
Community
  • 1
  • 1
user3571918
  • 139
  • 9
  • 1
    What did you try? Show your code. – Apurv Apr 28 '14 at 06:38
  • 3
    You are doing it wrong. Cells are the *View* in *MVC* and are for presentation and user interaction only. The checkmark is checked based on data in the *Model*, so if you follow *MVC* you will have quick, convenient, access to whatever condition set the checkmark. – trojanfoe Apr 28 '14 at 06:40

1 Answers1

1

Solution can be tricky :

Maintain one array same to tableview datasource array.

Say for example u have NSMutableArray *mutArrNames which tableview datasource. Now NSMutableArray *mutArrNamesCheckListwhich shows which one is checked or not;

Intially nothing is selected so add 0 to uncheck one:

for(int i =0; i<[mutArrNames count];i++)
{
    [mutArrNamesCheckList addObject:@"0"];
}

Now use like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   static NSString *CellIdentifier = @"Cell";
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

   if(section == 4) {
     //check from mutArrNamesCheckList's list if cell is check one or not
     if([[mutArrNamesCheckList objectAtIndex:indexPath.row] intValue] == 1) //checked one
     {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
     }
     else //unchecked one
     {
        cell.accessoryType = UITableViewCellAccessoryNone;
     }
   }
  }
  return cell;
}

Here it can be used according to your requirement like allow mutiple selection or single selection.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   //get cell object
   UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

   if(section == 4) 
   {
      if([[mutArrNamesCheckList objectAtIndex:indexPath.row] intValue] == 1) //checked one
      {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        //replace 0 with 1 as check one
        [mutArrNamesCheckList replaceObjectAtIndex:indexPath.row withObject:@"0"];
      }
      else //unchecked one
      {
        cell.accessoryType = UITableViewCellAccessoryNone;
        //replace 1 with 0 as uncheck one
        [mutArrNamesCheckList replaceObjectAtIndex:indexPath.row withObject:@"1"];
      }
   }
}

For selection and unselection of all cell add this in button's action method. Use this :

//remove all as new objects will be added
[mutArrNamesCheckList removeAllObjects];
if(!btnCheckAll.selected)//selected property used - default is no so all not selected
{
   //make all selected
   for(int i =0; i<[mutArrNames count];i++)
   {
    [mutArrNamesCheckList addObject:@"1"];
   }
   //change buttons image here
}
else
{
   //make all unselected
   for(int i =0; i<[mutArrNames count];i++)
   {
    [mutArrNamesCheckList addObject:@"0"];
   }
   //change buttons image here
}

//tableView reload
[yourtableView reloadData];

btnCheckAll.selected = !btnCheckAll.selected;

Add mutArrNamesCheckList in AppDelegate to be visible in whole Application.

So remove iteration we done on top of above answer in our viewcontroller as we didn't wanted to use gobally

EDIT : added toggling in didSelectRowAtIndexPath + clicked select all button, all cell is checked else unchecked

Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132