UITableViewCell: How to prevent blue selection background w/o borking isSelected property?

后端 未结 6 1063
予麋鹿
予麋鹿 2020-12-04 08:03

I have a custom UITableViewCell subclass. I have set the contentView of my cell subclass to a custom UIView class in which i am overr

相关标签:
6条回答
  • 2020-12-04 08:41

    An excellent resource on customizing UITableViews has been this post by Matt Gallagher. What you'll want to do is set the selectedBackgroundView to a new view (instead of nil) that is either transparent or a UIImageView.

    0 讨论(0)
  • 2020-12-04 08:41
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    0 讨论(0)
  • 2020-12-04 08:47

    How about this?

    // disable user interaction
    cell.userInteractionEnabled = NO;
    
    0 讨论(0)
  • 2020-12-04 09:01

    What has worked for me in the past is just putting:

    - (void)setSelected:(BOOL)selected animated:(BOOL)animated { }
    

    In my UITableViewCell subclasses (because it won't call super and make itself highlighted). Hope this is what you were looking for.

    0 讨论(0)
  • 2020-12-04 09:02

    By far the easier method - in my opinion - to achieve this, is by setting the Table View attributes checkbox in the Interface Builder screen where it says "Show Selection on Touch". See the screenshot below:

    enter image description here

    0 讨论(0)
  • 2020-12-04 09:04

    You must also override setHighlighted: to prevent the blue gradient from ever showing. If you just override setHighlighted: then you end up with a momentary selection effect.

    so you'll have these two methods:

    - (void)setHighlighted: (BOOL)highlighted animated: (BOOL)animated
    {
        // don't highlight
    }
    
    - (void)setSelected: (BOOL)selected animated: (BOOL)animated 
    {
        // don't select
        //[super setSelected:selected animated:animated];
    }
    
    0 讨论(0)
提交回复
热议问题