Xavi Valero
Xavi Valero

Reputation: 2057

Change background color of the button in UITableViewCell based on selection

I have a UITableView.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//Where we configure the cell in each row

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell;

cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}


self.myButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.myButton.tag = 5;
self.myButton.frame = CGRectMake(190.0f, 5.0f, 70.0f, 25.0f);
self.myButton.layer.borderColor = [UIColor blackColor].CGColor;
self.myButton.layer.borderWidth = 0.5f;
self.myButton.backgroundColor = [UIColor grayColor];
[self.myButton setTitle:@"Set Active" forState:UIControlStateNormal];
[self.myButton addTarget:self action:@selector(myButton:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview: self.myButton];

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

}

-(void) myButton:(id)sender{
[sender setBackgroundColor:[UIColor redColor]];
}

Assume, there 3 rows in the tableview section. I have the button in 3 rows. At the first instance, the button in the first row should have red color and the other 2 rows should have gray color. If I select the button in the 2nd row, then the button in the 2nd row should be red and the other 2 gray and so on. I haven't figured out a way to do it. Any suggestions?

I am able to change the color of the button I have selected from cell. But at the same instance, the previously selected button and all the other buttons(except the selected button) should be default color. At a time only one button will be highlighted, and that will always be the selected button at that instance.

Upvotes: 2

Views: 1965

Answers (4)

danylokos
danylokos

Reputation: 1623

I think the right way to do this - subclass UITableViewCell and declare own protocol that will inform delegate in which cell button was pressed, and in the delegate - loop through all cells and set default color for all and red color for selected. I made demo project

Upvotes: 1

Shah Paneri
Shah Paneri

Reputation: 739

Just try this code:

-(void) myButton:(id)sender{
    NSLog(@"sender Tag=== %d",[sender tag]);
    UIButton *btn=(UIButton *)sender;
    [btn setBackgroundColor:[UIColor redColor ]];

}

Upvotes: 0

deaDvo100shuB
deaDvo100shuB

Reputation: 31

Easy way is set different tag for each button. Like this:

button.tag = indexPath.row + c;

when 'c' is constant. And in you method:

-(void) myButton:(id)sender{
     for( int i=0; i<numberOfRowsInTable; ++i )
        [(UIButton *)[self.view viewForTag:i+c] setBackgroundColor:[UIColor grayColor]];
     [(UIButton *)sender setBackgroundColor:[UIColor redColor]];
}

But if you have 2 or more section in table this way may be not work

Upvotes: 1

funeralfunk
funeralfunk

Reputation: 163

Take a look at this: How can I loop through UITableView's cells?

Loop through the cells and set the "inactive" buttons to gray.

Upvotes: 0

Related Questions