-1

I have to implement multiple selection of the row from different section. So here is my code

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 EmailSelectionCell*cell;
cell = (EmailSelectionCell *)[tableView dequeueReusableCellWithIdentifier:@"EmailSelectionCell"];
if(cell == nil) {
    [[NSBundle mainBundle] loadNibNamed:@"EmailSelectionCell" owner:self options:nil];
    cell = EmailSelectionCell;
}

NSArray *insuranceEmailArray = [[data objectAtIndex:indexPath.section] objectForKey:@"email"];


   [((EmailSelectionCell *) cell).lbl_name setText:[insuranceEmailArray objectAtIndex:indexPath.row]];

return cell;
}

In tableview cell

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{

[super setSelected:selected animated:animated];

if(selected){
    imgv_selectionBox.image = [UIImage imageNamed:@"tick_selected"];
}else{
    imgv_selectionBox.image = [UIImage imageNamed:@"tick_unselected"];
}

}

My problem is that setSelected is calling twice when i debug and one more problem is that it is behaving like single selection. I am thinking the problem is due to section but i don't know how to handle it. So please help to resolve this kind of problem.

Gaurav Patel
  • 532
  • 1
  • 5
  • 19

2 Answers2

1

You are overriding a method on cell setSelected while you should be implementing a delegate method didSelectRowAt. You can also get selected index paths at any time from table view by using indexPathsForSelectedRows.

There is a setting to have exclusive row selection or to be able to select multiple rows with allowsMultipleSelection. So make sure this one is set to YES.

Your overridden method still seems legit but I am unsure if it should work as you implemented it. As you say the method is called multiple times. For your case to work it should also be called when you scroll through the table view. The reason for that is that your cells are being dequeued (it is how table view works) which means a same cell will be used for multiple rows; when you scroll down a cell that will disappear on top will in fact appear on bottom as reused. Your cellForRowAtIndexPath will be called so it might be you will need to change your icon there.

Matic Oblak
  • 16,318
  • 3
  • 24
  • 43
0

setSelected method will call twice, because one call is to deselect previous selection and second one is for currently selected one. Actually you should not manage multiple selection in this method. You can manage that in didSelectRowAtIndexpath: method. Do your changes in didSelectRowAtIndexpath method. And add your selected indexPaths into an array. So, if you scroll the table view, you can manage the selection.

   func tableView(_ tableView: UITableView, cellForRowAt indexPath:IndexPath) -> UITableViewCell {

      =======
      cell initialisation
      =======

      if indexPathsArray.contains(indexPath) {
         imgv_selectionBox.image = [UIImage imageNamed:@"tick_selected"];
      }else{
         imgv_selectionBox.image = [UIImage imageNamed:@"tick_unselected"];
      }
     }

     func tableView(_ tableView: UITableView, didSelectRowAt indexPath:  IndexPath)
     {
        if indexPathsArray.contains(indexPath) {
         ===remove index path here from array====
          imgv_selectionBox.image = [UIImage imageNamed:@"tick_selected"];
        }else{
         ====add index path to array
          imgv_selectionBox.image = [UIImage imageNamed:@"tick_unselected"];
        }
     }
Hari
  • 144
  • 9