How do I set UITableViewCellSelectionStyle property to some custom color?

前端 未结 9 1762
栀梦
栀梦 2020-12-13 04:04

I am developing an iPhone application, in my table view I wanted custom color for Cell Selection Style, I read the UITableViewCell Class Reference but there are onl

9条回答
  •  半阙折子戏
    2020-12-13 04:45

    Sublcass UITableViewCell and override setHighlighted:animated:

    You can define a custom selection color color by setting the backgroundColor (see WIllster's answer):

    - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
        if(highlighted) {
            self.backgroundColor = [UIColor redColor];
        } else {
            self.backgroundColor = [UIColor clearColor];
        }
    
        [super setHighlighted:highlighted animated:animated];
    }
    

    You can define a custom background image by setting the backgroundView property:

    - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
        if( highlighted == YES )
            self.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"seasonal_list_event_bar_default.png"]];
        else
            self.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"seasonal_list_event_bar_active.png"]];
    
    
        [super setHighlighted:highlighted animated:animated];
    }
    

提交回复
热议问题