开发者

Remove highlight color but maintain image highlight in Custom UITableViewCell

开发者 https://www.devze.com 2023-01-17 06:00 出处:网络
I am searching this for some time but I never found a solution. I have a UITableVie开发者_运维技巧w with custom cells, those cells have an image and I place the labels on it.

I am searching this for some time but I never found a solution.

I have a UITableVie开发者_运维技巧w with custom cells, those cells have an image and I place the labels on it.

The thing is, when I select a cell, the whole cell frame is selected using the default highlight color. I tried changing the style, but if I use cell.selectionStyle = UITableViewCellSelectionStyleNone; the image won't highlight...

Just in case someone asks I created distincts images for highlighted / normal states.


There is a property named highlighted in UITableViewCell.

@property(nonatomic, getter=isHighlighted) BOOL highlighted

So, you can modify the highlighted behavior by overwriting this method.

- (void)setHighlighted:(BOOL)value
{
   [super setHighlighted:value];

   // Do custom highlighted behavior here
   ...
}

Or overwrite setHighlighted:animated: method.


Something like this?

In MyCustomCell.m

- (void)setHighlighted:(BOOL)value
{
   [super setHighlighted:value];

   // Do custom highlighted behavior here
   if (value == YES)
   {
        // show highlighted image 
   } else {
        // show image for normal state. 
   }

   // propagate the highlighted message to other views.
   [mChildView setHighlighted:value];
}


Here's how I made it work:

I'm using a XIB for my cell. In there I have a background image (not connected to any outlets) that is my normal-state background. I set that image to also have a highlighted image (a blue version in my case).

I added another UIView, set its background color to clear, and attached it to the cell's selectedBackgroundView outlet. Doing that prevents the normal built-in "set the entire background blue" behavior.

Then I set the cell's selection style to blue (just can't be None as the highlighting won't happen then).


I found a very good solution.

I didn't had to override any methods.

1 - In my CustomCell.xib I created 2 imageViews. One I connected to myImageView outlet, and the other to selectedBackgroundView.

2 - For the normal state I put myCustomBackgroundImg.png and in the Highlighted image I put a empty png, so it won't stay on top of the backgroundView when it's selected.

3 - And in the selectedBackgroundView I put myCustomSelectedBackgroundImg.png in the normal state image.

Hope this helps others.


Improved Toro's code.

It works!

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {

    if (highlighted == YES) {
        // show highlighted image
        [UIView animateWithDuration:0.1f animations:^{
            self.imageViewBanner.alpha = 0.3;
        }];
    } else {
        // show image for normal state.
        [UIView animateWithDuration:0.1f animations:^{
            self.imageViewBanner.alpha = 1.0;
        }];
    }

    [super setHighlighted:highlighted animated:animated];
}


Best solution I could find was to replace cell's selectedBackgroundView with a transparent view, and leaving selectionStyle to UITableViewCellSelectionStyleBlue, so that highlighted state is still propagated to all subviews (using UITableViewCellSelectionStyleNone stops highlight propagation to subviews).

UIView *transparentBackground = [[UIView alloc] initWithFrame:cell.bounds];
transparentBackground.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
transparentBackground.backgroundColor = [UIColor clearColor];
cell.selectedBackgroundView = transparentBackground;


100% working and simple solution:

Step 1: set cell.selectionStyle = UITableViewCellSelectionStyleNone; in code or in InterfaceBuilder.
Step 2: add 2 methods:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.imageView.highlighted = YES;
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.imageView.highlighted = NO;
}

in Swift:

cell?.selectionStyle = .None


override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let cell = tableView.cellForRowAtIndexPath(indexPath)
    cell?.imageView?.highlighted = true;
}

override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
    let cell = tableView.cellForRowAtIndexPath(indexPath)
    cell?.imageView?.highlighted = false;
}
0

精彩评论

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

关注公众号