Why do all backgrounds disappear on UITableViewCell select?

前端 未结 8 1881
南笙
南笙 2020-12-02 14:22

My current project\'s UITableViewCell behavior is baffling me. I have a fairly straightforward subclass of UITableViewCell. It adds a few extra elements to the base view (vi

8条回答
  •  一生所求
    2020-12-02 14:50

    All we need is to override the setSelected method and change the selectedBackgroundView for the tableViewCell in the custom tableViewCell class.

    We need to add the backgroundview for the tableViewCell in cellForRowAtIndexPath method.

    lCell.selectedBackgroundView = [[UIView alloc] init];
    

    Next I have overridden the setSelected method as mentioned below.

    - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
    
    UIImageView *lBalloonView = [self viewWithTag:102];
    [lBalloonView setBackgroundColor:[[UIColor hs_globalTint] colorWithAlphaComponent:0.2]];
    
    UITextView *lMessageTextView = [self viewWithTag:103];
    lMessageTextView.backgroundColor    = [UIColor clearColor];
    
    UILabel *lTimeLabel = [self viewWithTag:104];
    lTimeLabel.backgroundColor  = [UIColor clearColor];
    
    }
    

    Also one of the most important point to be noted is to change the tableViewCell selection style. It should not be UITableViewCellSelectionStyleNone.

    lTableViewCell.selectionStyle = UITableViewCellSelectionStyleGray;
    

提交回复
热议问题