iOS UITableViewCell點擊時子視圖背景透明的解決方案

來源:互聯網
上載者:User

標籤: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點擊時子視圖背景透明的解決方案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.