I have tableview , where I subclassed tableviewcell. There is a horizontal scrollview in the cell and I add dynamic button to the scrollview.
My requirement: 1. When I tap the button(s) on row0 for the first time, I need to set a different BG color for the tapped button and add the button index in the array. When I tap the same button the second time, I need to clear the BG of the tapped button and remove the button index from the array - I'm able to achieve this 2. Let's say, I have selected two buttons in row0 and when I try to tap some button on row1, I should clear the BG color of all the selected button in row0, clear the array and set the BG color of the tapped button of row2 and add this button index in the same array - I have big problem achieving this.
In the above-attached image, I have selected "Test 0" and "Test 1" buttons in "room 1". Now when I tap "Test 0" or "Test 1" button in "room 2", I should clear all the selected buttons in "room 1" and select/set BG color of tapped button in "room 2".
// Updated code 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];
}
}
}