UIButton added to UITableViewCell content view only appears when cell is selected

I'm having problems adding buttons to the content view of a UITableViewCell. The button is not visible. But, when I click on a cell and when it is selected (using bluebackground by default), the button may be visible.

The table view controller is created using initWithStyle:UITableViewStyleGrouped .

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UIButton *theButton; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; theButton = [[UIButton alloc] initWithFrame:CGRectMake(800, 25, 32, 32)]; [cell.contentView addSubview:theButton]; theButton.tag = 1; [theButton release]; } if(indexPath.section ==0){ //...logic... }else if(indexPath.section ==1){ //...logic... }else{ cell.textLabel.text = @"some string"; theButton = (UIButton*)[cell.contentView viewWithTag:1]; [theButton setImage:theImage forState:UIControlStateNormal]; } return cell; } 
+4
source share
1 answer

I solved the problem by clearing the background color of the label of the table cell label using [UIColor clearColor] .

+4
source

All Articles