溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

UITableViewCell在編輯狀態(tài)下如何修改背景顏色

發(fā)布時(shí)間:2021-07-23 09:24:34 來源:億速云 閱讀:134 作者:小新 欄目:移動(dòng)開發(fā)

小編給大家分享一下UITableViewCell在編輯狀態(tài)下如何修改背景顏色,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

一、先看下效果圖

UITableViewCell在編輯狀態(tài)下如何修改背景顏色

二、網(wǎng)上很多下面這種答案

UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;

這樣設(shè)置,藍(lán)色的選中圖標(biāo)也不會(huì)出現(xiàn).

這種僅限于不編輯的時(shí)候,讓TableViewCell沒有灰色高亮.

三、具體實(shí)現(xiàn):

(1).在創(chuàng)建cell的時(shí)候設(shè)置selectedBackgroundView

RealTimeControlTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];

if (cell == nil) {
 cell = [[RealTimeControlTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId];
 cell.contentView.backgroundColor = [UIColor clearColor];
 UIView *backGroundView = [[UIView alloc]init];
 backGroundView.backgroundColor = [UIColor clearColor];
 cell.selectedBackgroundView = backGroundView;
}

(2).自定義一個(gè)UITableVIewCell重寫

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
if (!self.editing) {
 return;
}
[super setSelected:selected animated:animated];

if (self.editing) {
 self.contentView.backgroundColor = [UIColor clearColor];
 self.textLabel.backgroundColor = [UIColor clearColor];
 self.detailTextLabel.backgroundColor = [UIColor clearColor];
}
}

(3)還要重寫下面方法 因?yàn)樵陂L(zhǎng)按cell的時(shí)候也會(huì)高亮,出現(xiàn)灰色的背景

-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
 return;
}

對(duì)上面第二步代碼說明:

1.在非編輯狀態(tài)下,默認(rèn)不會(huì)出現(xiàn)選中效果,直接return.

return 以后還是會(huì)繼續(xù)調(diào)用

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
 在這里處理cell的點(diǎn)擊事件
}

2.要實(shí)現(xiàn)選中的藍(lán)色圖標(biāo)出現(xiàn),以及添加cell到選中cell的數(shù)組.

調(diào)用系統(tǒng)的默認(rèn)方法

[super setSelected:selected animated:animated];

3.在編輯狀態(tài)下修改cell的contenView為clear,清除選中時(shí)候的灰色背景.

看完了這篇文章,相信你對(duì)“UITableViewCell在編輯狀態(tài)下如何修改背景顏色”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI