Botón Tableviewcell

Tengo tableview, donde subclasifiqué tableviewcell. Hay una vista de desplazamiento horizontal en la celda y agrego un botón dinámico a la vista de desplazamiento.

Mi requisito: 1. Cuando toco los botones en la fila 0 por primera vez, necesito establecer un color BG diferente para el botón tocado y agregar el índice del botón en la matriz. Cuando toco el mismo botón la segunda vez, necesito borrar el BG del botón tocado y eliminar el índice del botón de la matriz. Puedo lograr esto 2. Digamos que he seleccionado dos botones en la fila 0 y cuando Intento tocar algún botón en la fila1, debería borrar el color BG de todos los botones seleccionados en la fila0, borrar la matriz y establecer el color BG del botón girado de la fila2 y agregar el índice de este botón en la misma matriz. problema para lograr esto.

En la imagen adjunta, seleccioné los botones "Prueba 0" y "Prueba 1" en la "sala 1". Ahora, cuando toque el botón "Prueba 0" o "Prueba 1" en "habitación 2", debería borrar todos los botones seleccionados en "habitación 1" y seleccionar / configurar el color BG del botón girado en "habitación 2".

// Código actualizado Mysolution:

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
      static NSString *cellIdentifier = @"roomCells";

      cell = (roomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

  UIButton *slotButton;
  cell.timeslotScrollView.contentSize = CGSizeMake(500, 0);
  cell.timeslotScrollView.scrollEnabled = YES;
  cell.timeslotScrollView.showsHorizontalScrollIndicator = NO;
  int buttonSpace = 10;
  int buttonWidth = 68;
  int buttonHeight = 35;
  int yPosition = 0;
  float xPosition = 0;
  for (int i=0; i<=1; i++) {
    slotButton = [[UIButton alloc]init];
    slotButton.frame = CGRectMake(xPosition, yPosition, buttonWidth, buttonHeight);
    [slotButton setTitle:[NSString stringWithFormat:@"Test %d",i] forState:UIControlStateNormal];
    [slotButton setTitleColor:[UIColor colorWithRed:0.23 green:0.71 blue:0.29 alpha:1.0] forState:UIControlStateNormal] ;
    slotButton.layer.borderWidth = 2;
    slotButton.layer.borderColor = [UIColor colorWithRed:0.23 green:0.71 blue:0.29 alpha:1.0].CGColor;
    slotButton.layer.cornerRadius = 3;
    slotButton.tag = i;

    slotButton.userInteractionEnabled = YES;
    [slotButton addTarget:self action:@selector(didTap:) forControlEvents:UIControlEventTouchUpInside];
    xPosition = slotButton.frame.origin.x+buttonWidth+buttonSpace;

    if (cell.timeslotScrollView.subviews.count < numberOfItems)
    {
      [cell.timeslotScrollView addSubview:slotButton];
    }
  }

      }

      return cell;
    }


Updated Action method:

-(void)didTap:(id)sender
{
  static int buttonTapCount = 0;
  buttonTapCount++;
  NSMutableArray *toDelete = [NSMutableArray new];
  NSMutableArray *toAdd = [NSMutableArray new];
  UIButton *pressedButton = (UIButton *)sender;
  CGPoint touchPoint = [pressedButton convertPoint:CGPointZero toView:self.roomTableView];
  NSIndexPath *indexOfButton = [self.roomTableView indexPathForRowAtPoint:touchPoint];
  NSInteger buttonIndex = pressedButton.tag;
  NSString *buttonIndexString = [NSString stringWithFormat:@"%ld",(long)buttonIndex];

if (buttonTapCount==1)
{
tappedButtonRowIndex = indexOfButton.row;
[toAdd addObject:buttonIndexString];
[pressedButton setBackgroundColor:[UIColor greenColor]];
}
else if(buttonTapCount>=1)
{
  if (tappedButtonRowIndex==indexOfButton.row)
  {
      if (buttonSelectedArray.count==0)
      {
      [toAdd addObject:buttonIndexString];
      [pressedButton setBackgroundColor:[UIColor greenColor]];
      }
      else
      {
      isButtonAlreadySelected = false;
      for (NSString  *value in buttonSelectedArray)
      {
        if ([buttonIndexString isEqualToString:value])
        {
        [toDelete addObject:value];
        [pressedButton setBackgroundColor:[UIColor clearColor]];
        isButtonAlreadySelected = true;
        }
      }
      }
  }
  else
  {
  if (buttonSelectedArray.count==0)
      {
      [toAdd addObject:buttonIndexString];
      [pressedButton setBackgroundColor:[UIColor greenColor]];
      tappedButtonRowIndex = indexOfButton.row;
      isButtonAlreadySelected=true;
      }
      else
      {
        isButtonAlreadySelected=true;

      UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"Please deselect the previous selected slots" preferredStyle:UIAlertControllerStyleAlert];

      [alertController addAction:[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleCancel handler:nil]];

      [self presentViewController:alertController animated:YES completion:nil];

  }
  }
}

Respuestas a la pregunta(0)

Su respuesta a la pregunta