Iphone – Selected UIButton disappears inside selected UITableViewCell

iphoneuibuttonuitableview

I have a table, I have cellForRowAtIndexPath delegate for it and I have instance of UITableViewCell being created inside that method, which I return. Somewhere in cellForRowAtIndexPath I also add UIButton to cell.contentView of that cell. It all works fine, until I select that cell with button in it. The cell changes color to blue (which is ok), uibutton changes its color to blue too. Now, if I click on that UIButton inside selected cell, it just disappears! Wow, that's weird, how do I fix it? I want UIButton inside selected cell to stay, when I click it.

Edit: included my cellForRowAtIndexPath implementation

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
static NSString *sectionsTableIdentifier = @" sectionsTableIdentifier ";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: sectionsTableIdentifier];


    for (UIView *view in cell.contentView.subviews)
      [view removeFromSuperview];


     if (cell == nil) {
      cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero 
                reuseIdentifier: sectionsTableIdentifier] autorelease];

     }

     CGRect newIconRect = CGRectMake(276, 40, 29, 29);
      UIButton *newIcon = [[UIButton alloc] initWithFrame:newIconRect];
      [newIcon setImage:[UIImage imageNamed:@"mynewicon.png"] forState:UIControlStateNormal];
      [cell.contentView addSubview:newIcon];
      [newIcon release];


    return cell;

    }

Best Answer

I noticed a similar issue on OS 4.0 beta where I had a UIButton in a UITableViewCell with setImage:forState:UIControlStateNormal. The button would disappear when I would return to the table view after selecting a row and pushing another table view. To overcome this, I setImage:forState:UIControlStateHighlighted on the image as well. I'm not sure if this would resolve your particular issue, but it did for me. I think also setting the button.highlighted=NO in didSelectRowAtIndexPath: might have worked as well, but I didn't try it.

Related Topic