开发者

UIButton in UITableViewCell

I have a UIButton with an image inside of a UITableViewCell. When the cell is being highlight, the button is also entering the highlighted state (i.e. a darker shade of the image), regardless of whether the user is clicking within the bounds of the button or not.

I don't want this functionality - I only want the button to be highlighted when the button is clicked, not when the entire cell is being clicked.

I've tried to set the image in the highlighted state to be the same as the no开发者_运维百科rmal image. This fixes the issue however it stops the button from changing color when it really is highlighted.

Any ideas how to achieve the desired effect?


This was driving me crazy. I figured out that you need to override setHighlighted:animated: and setSelected:animated:

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
    [super setHighlighted:highlighted animated:animated];
    self.yourButton.highlighted = NO;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    self.yourButton.selected = NO;
    // If you don't set highlighted to NO in this method,
    // for some reason it'll be highlighed while the 
    // table cell selection animates out
    self.yourButton.highlighted = NO;
}


One approach would be to "deselect" or "unhighlight" the button when the table view cell is selected:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  [yourButton setHighlighted:NO];
  // do something cool
}


codecaffeine's suggestion didn't work for me (iOS 8.3), but it did put me on the right track. I modified it like this (it's in Swift though):

override func setHighlighted(highlighted: Bool, animated: Bool) {
    var colorBefore = self.myButton.backgroundColor
    super.setHighlighted(highlighted, animated: animated)
    self.myButton.highlighted = false
    self.myButton.backgroundColor = colorBefore
}


I've used a different approach, it's a little bit easier, I hope it works for you. Just set the button's highlighted state to false inside the two above delegate methods:

-(void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
    UIButton *btnAction = (UIButton *) [[tableView cellForRowAtIndexPath:indexPath] viewWithTag:3];
    btnAction.highlighted = NO;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UIButton *btnAction = (UIButton *) [[tableView cellForRowAtIndexPath:indexPath] viewWithTag:3];
    btnAction.highlighted = NO;
}


Haven't actually tried this, but could you maybe add a target / action to the button for UIControlEventTouchDown that updated its highlighted-state image to what you wanted, then another target / action for UIControlEventTouchUpInside / UIControlEventTouchUpOutside / UIControlEventTouchCancel that reset the highlighted image to match the normal-state image?


A possible workaround would be that, you set the cell selection style to be none. In that case when you select the cell, it would not be highlighted.

This is only, the possible workaround. May be you have other things in your mind.


To make it work, I had to use the following subclassing on the UITableViewCell. The 'button' object is the button inside the custom cell:

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
    [self.button setShowsTouchWhenHighlighted:NO];
    [super setHighlighted:highlighted animated:animated];

    self.button.highlighted = NO;
    [self.button setShowsTouchWhenHighlighted:YES];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [self.button setShowsTouchWhenHighlighted:NO];
    [super setSelected:selected animated:animated];
    self.button.highlighted = NO;

    // Configure the view for the selected state
    [self.button setShowsTouchWhenHighlighted:YES];
}

Interverting the various line may result in the highlighting of the button.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜