I’m having problems trying to add buttons into a UITableViewCell’s content view.
The button cannot be seen. But, when I click on the cell and when it is selected (with the default bluebackground), the button can be seen.
The table view controller is created with: 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;
}
I solved the problem by making the table view cell’s text label background color clear with
[UIColor clearColor].