UIView backgroundColor disappears when UITableViewCell is selected

前端 未结 17 1080
谎友^
谎友^ 2020-11-30 17:49

I have a simple tableViewCell build in interface builder. It contains a UIView which contains an image. Now, when I select the cell, the default blue selection background is

17条回答
  •  执念已碎
    2020-11-30 18:23

    You can change the behavior of the tableViewCell by overriding the function setHighlighted in UITableViewCell class (you will need to inherit from it). Example of my code where I change the background image of my cell :

    // animate between regular and highlighted state
    - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated; {
        [super setHighlighted:highlighted animated:animated];
    
        //Set the correct background Image
        UIImageView* backgroundPicture = (UIImageView*)[self viewWithTag:HACK_BACKGROUND_VIEW_TAG];
        if (highlighted) {
            backgroundPicture.image = [UIImage imageNamed:@"FondSelected.png"]; 
        }
        else {
            backgroundPicture.image = [UIImage imageNamed:@"Fond.png"]; 
        }
    }
    

    You can also change the selection mode to gray, blue or none in the interface builder.

提交回复
热议问题