Botão da célula da tableview
Tenho tableview, onde subclassifiquei a tableviewcell. Há uma exibição de rolagem horizontal na célula e adiciono botão dinâmico à exibição de rolagem.
Minha exigência: 1. Quando toco os botões na linha 0 pela primeira vez, preciso definir uma cor BG diferente para o botão pressionado e adicionar o índice de botões na matriz. Quando toco no mesmo botão na segunda vez, preciso limpar o BG do botão pressionado e remover o índice de botões da matriz - sou capaz de conseguir isso 2. Digamos que selecionei dois botões na linha0 e quando Eu tento tocar em algum botão na linha1, devo limpar a cor BG de todos os botões selecionados na linha0, limpar a matriz e definir a cor BG do botão pressionado da linha2 e adicionar esse índice de botão na mesma matriz - tenho grande problema em conseguir isso.
Na imagem anexada acima, selecionei os botões "Teste 0" e "Teste 1" na "sala 1". Agora, quando toco no botão "Teste 0" ou "Teste 1" na "sala 2", devo limpar todos os botões selecionados na "sala 1" e selecionar / definir a cor BG do botão pressionado na "sala 2".
// Código atualizado 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];
}
}
}