tags:

views:

41

answers:

2
+2  A: 

You have to set cell.accessoryType in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

The fact is that as you have a reuse identifier, when a new cell is requested, a cell with the identifier is copied. As you have set the checkmark in the latest cell, the checkmask is copied.

You have to store the state of each cell (ie in an array) to recreate the cell with the right value.

Something like that:

BOOL values[50]; //Not the best way, but easy for the example...


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  static NSString *CellIdentifier = @"Cell";
  TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil) {
    cell = [[[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
  }
  cell.textLabel.text=[NSString stringWithFormat:@"%i",[indexPath row]];
  if (value[indexPath.row]) { 
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
  } else {
    cell.accessoryType = UITableViewCellAccessoryNone;
  }
  return cell;
}


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

  values[indexPath.row] = !value[indexPath.row];

  if (value[indexPath.row]) { 
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
  } else {
    cell.accessoryType = UITableViewCellAccessoryNone;
  }
}
Benoît
+1 cant understand how to this this method is called every time
pankaj kainthla
it worked thanks accepted
pankaj kainthla
great one keep coding
pankaj kainthla
+1  A: 

You can do it with the help of array , take one array and each array hold a dict do this condition in array
NSMutableArray *arr = [[NSMutableArray alloc]init]; for(int i=1; i<=50;i++) { NSmutableDictionary *dict = [[NSMutableDictionary alloc]init]; [dict setobject:[nsstring stringwithformat:@"%d",i] valueForkey:@"ID"]; [arr addobject:dict]; [dict release]; }

now rows is [arr count]; and put this check in cell appearence nsmutabledict *dict1 = [arr objectatindex:indexpath.row]; if([[dict objectforkey:"Check"]==nil]) { [dict setobject:[nsstring stringwithformate:@"1"] valueforkey:@"Check"]; }else { if([[dict objectforkey:"Check"] isequaltostring:@"1"] { cell.accessoryType = UITableViewCellAccessoryNone; } else { cell.accessoryType = UITableViewCellAccessoryCheckmark; } }

not put below code in didselect methood nsmutabledict *dict1 = [arr objectatindex:indexpath.row]; if([[dict objectforkey:"Check"] isequaltostring:@"1"] { [dict setobject:[nsstring stringwithformate:@"2"] valueforkey:@"Check"]; } else { [dict setobject:[nsstring stringwithformate:@"1"] valueforkey:@"Check"]; }

GhostRider
+1 ur formatting is very complex .i cant understand some things ,1)why is check (key) for (2)u stored array in dict1 but never used it and only use dict
pankaj kainthla