Pascal Pascal - 5 months ago 22
iOS Question

UITableView selectable list

I am trying to implement a selectable list view. The list view contains some names. You can select or deselect multiple names. If a name is selected the cell style is changed to

UITableViewCellAccessoryCheckmark
.

But now I have a problem. Before you can deselect a name you have to press multiple times onto the cell. This is because
didSelectRowAtIndexPath
gets always called first.

// cellForRowAtIndexPath function
if ([assignedPlayers containsObject:player.persistentData])
{
[cell setSelected:true];
[cell setAccessoryType: UITableViewCellAccessoryCheckmark];
}

cell.textLabel.text = [NSString stringWithFormat:@"%@", player.persistentData.name];

return cell;
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
[[tableView cellForRowAtIndexPath:indexPath] setSelected:FALSE];
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType: UITableViewCellAccessoryNone];

[self.eventController removePlayerFromEvent:_editingEvent :[self.playerController getPlayerAtPosition:indexPath.row]];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[[tableView cellForRowAtIndexPath:indexPath] setSelected:TRUE];
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType: UITableViewCellAccessoryCheckmark];

[self.eventController addPlayerToEvent:_editingEvent :[self.playerController getPlayerAtPosition:indexPath.row]];
}


Now I don't know how to fix this.

thanks for your help.

Answer

Have a look at the example below where _selectedList contains the selected players and _datasoureArray is the datasource that contains all the players names.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
  if(cell == nil)
   {
      cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELL"];
   }
   cell.textLabel.text = [_datasoureArray objectAtIndex:indexPath.row];
   if([_selectedList containsObject:[_datasoureArray objectAtIndex:indexPath.row]]) //hear selected list is an mutable array wich holds only selected player
    {
      cell.selected = YES;
      cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else
    {
      cell.selected = NO;
      cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
   if(cell)
   {
      if(cell.accessoryType == UITableViewCellAccessoryNone)
      {
        [_selectedList addObject:[_datasoureArray objectAtIndex:indexPath.row]];//add
      }
      else
      {
        if([_selectedList containsObject:[_datasoureArray objectAtIndex:indexPath.row]])
       {
           [_selectedList removeObject:[_datasoureArray objectAtIndex:indexPath.row]]; //remove
       }
     }
 }
 [tableView reloadData];
 NSLog(@"%@",_selectedList.description);
}
Comments