ArisRS
ArisRS

Reputation: 1394

UITableView save selected items

I need to save selected items for my UITableView in the NSMutableDictionary for iplementing checkboxes. So I do the following:

  NSMutableDictionary * selDict;// - instance variable

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

      if ([selDict objectForKey:indexPath])
          [selDict setObject:[NSNumber numberWithBool:FALSE] forKey:indexPath];
      else
          [selDict setObject:[NSNumber numberWithBool:TRUE] forKey:indexPath];

      [[self tableView]reloadData];

  }

  - (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] autorelease];
          [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
      }

      if ([selDict objectForKey:indexPath])
          cell.accessoryType = UITableViewCellAccessoryCheckmark;
      else
          cell.accessoryType = UITableViewCellAccessoryNone;
      ...

      return cell;
   }

But it is only sets the items checked and does't work further.

Upvotes: 0

Views: 510

Answers (1)

Deepak Danduprolu
Deepak Danduprolu

Reputation: 44633

You might want to change

if ([selDict objectForKey:indexPath])

to

if ([[selDict objectForKey:indexPath] boolValue])

What you are doing now is checking whether the object exists or not. It will work once when it is nil but not after that.

Upvotes: 2

Related Questions