標籤:anim 自訂 margin 解決問題 frame select 重寫 顏色 十分
在做iOS項目的開發中,UITableView控制項的應用十分廣泛。在進行自訂UITableViewCell時,經常遇到這樣的問題:在UITableViewCell上面添加了一個有背景顏色的子視圖,當使用者點擊UITableViewCell或者選中UITableViewCell時,Cell上的子視圖發生了奇怪的變化,其背景色變透明了,如果添加在Cell上的子視圖只是一個色塊,那麼我們看起來,這個子視圖好像莫名其妙的消失了一樣.
如果設定 self.selectionStyle = UITableViewCellSelectionStyleNone; 這也能解決問題。
但是如果要求點擊cell有背景色,上面的方法就沒用了。(提供設定cell的背景的方法 self.selectedBackgroundView = [[UIView alloc] initWithFrame:self.frame]; self.selectedBackgroundView.backgroundColor = [UIColor redColor]),這時候我們只需要重寫cell的兩個父類的兩個方法,重新設定子視圖的背景色即可。
//這個方法在Cell被選中或者被取消選中時調用- (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; self.markLb.backgroundColor = [UIColor blueColor];}//這個方法在使用者按住Cell時被調用-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{ [super setHighlighted:highlighted animated:animated]; self.markLb.backgroundColor = [UIColor blueColor];}
iOS UITableViewCell點擊時子視圖背景透明的解決方案