开发者

Selected UIButton disappears inside selected UITableViewCell

开发者 https://www.devze.com 2022-12-26 23:45 出处:网络
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 UIBu

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 allo开发者_运维知识库c] 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;

    }


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.


It appears that the button is somehow inheriting the selected state as its highlighted state. Anyone ever find a solution to this?

SOLUTION: I'm not exactly happy about this hack but it works for my purposes. Basically, I want the button to appear only when the cell is selected. Subclass the UITableViewCell and add these overrides (editButton is the button instance that was added to the cell's contentView in init).

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    editButton.hidden = YES;

    [super setSelected: selected
              animated: animated];

    editButton.highlighted = NO;

    if (selected)
        [editButton performSelector: @selector(setHidden:)
                         withObject: nil
                         afterDelay: 0.2];
}

- (void)setSelected:(BOOL)selected {
    editButton.hidden = YES;

    [super setSelected: selected];

    editButton.highlighted = NO;

    if (selected)
        [editButton performSelector: @selector(setHidden:)
                         withObject: nil
                         afterDelay: 0.2];
}


Hey, I know this is an old post but this can be helpful : UITableView will try to propagate its selection state in the whole hierarchy of its subviews. If one subview responds to -setHighlighted: it will be updated according to the cell selection state.

That explains why the button becomes blue (highlighted), but also why the button image disappears sometimes : a UIButton contains UIImageView instances to display its images and will swap the image of each image view according to its state (-[UIControl state]). But the table view cell comes and sets the highlighted flag of the button's image view to YES (which the button did not foresee), but this image view has no image for its highlighted state, so you see nothing, even if the button keeps swapping images according to its state.

The solution I used was, whenever the cell is selected or highlighted, I set the highlighted flag of the button's image view to NO.


I had trouble as well with this (for 4.3 and less) and the solutions above didn't work for me. Finally, setting button.adjustsImageWhenHighlighted = NO; when creating the button did the trick for me.


The trick is to add UIButton as a subview onto UIImageView.

  1. Create UIImage

    UIImage backgroundImg = [UIImage imageName:@"yourimg"];
    UIImageView imageView = [UIImageView alloc]initWithImage:backgrounding];
    
  2. Now create the button

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    // set the frame. the position of the button is the center 
    CGRect newIconRect = CGRectMake(imageView.center.x, imageView.center.y, 29, 29);
    
    button.frame = newIconRect;
    
  3. Finally add the button onto UIImageView

    [imageView addSubview:button];
    [cell addSubview imageView];
    

Now you dont have to play with the highlight color.

0

精彩评论

暂无评论...
验证码 换一张
取 消