1. 程式人生 > >當UITableViewCell被選中時,上面的自定義view消失的問題

當UITableViewCell被選中時,上面的自定義view消失的問題

view並沒有消失,只是變透明瞭而已。

下面是從蘋果官方文件拷貝的:
UITableViewCell changes the background color of all sub views when cell is selected or highlighted.
意思就是說當UITableViewCell被選中或者高亮的時候,它的所有子view的顏色都會改變。

如果你不喜歡讓它變透明,你可以在你的自定義UITableViewCell裡重寫這兩個方法:

假設那個view叫redView,它的顏色為紅色。

// CustomTableViewCell.h

@interface CustomTableViewCell : UITableViewCell



@property (weak, nonatomic) UIView *redline;

@end



// CustomTableViewCell.m

@implementation CustomTableViewCell

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

    _redView.backgroundColor = [UIColor redColor];
}

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

    [super setSelected:selected animated:animated];

    _redView.backgroundColor = [UIColor redColor];
}

@end

意即當選中或高亮時都顯示它原本的顏色,這樣就不會"消失"啦!