[英]UITableViewCell - setting background view and accessory in the initializer
[英]Setting custom color for background of UITableViewCell accessory
我正在嘗試讓我的表格視圖單元格在選中時顯示自定義選中標記圖像。 問題是,選中標記圖像后面的顏色是灰色,而不是我提供的自定義單元格顏色。
我知道之前曾有人問過這個問題-我已通讀並嘗試了以下建議:
其中。 總體共識似乎是,更改單元格backgroundView
的顏色會影響附件,但對我而言似乎並非如此。 這是我當前的代碼:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryView = [[UIImageView alloc] initWithImage:_checkImage];
[self setCellColor:_customColor forCell:cell];
}
- (void)setCellColor:(UIColor *)color forCell:(UITableViewCell *)cell
{
cell.contentView.backgroundColor = color;
cell.backgroundView.backgroundColor = color;
cell.backgroundColor = color;
}
請注意,我的checkImage
具有透明背景。 選擇單元格后,單元格的背景將更改為customColor
,但附件上的背景仍為灰色。 SOS!
普遍的共識似乎是,更改單元格backgroundView的顏色會影響附件
那是對的。 問題是您沒有這樣做。 你是這樣說的:
cell.backgroundView.backgroundColor = color;
但是cell.backgroundView
為nil,所以該行毫無意義。 您需要先為單元格提供背景視圖,然后才能設置該視圖的顏色。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.