2012-01-01 53 views
6

我試圖更改當您滑動UITableViewCell行時出現的視圖的背景顏色,這是'刪除'按鈕後面的背景顏色。在UITableViewCell中滑動以刪除具有白色背景,需要清除

我試着改變cell.editingAccessoryView,但沒做任何事情。

UIView* myBackgroundView = [[UIView alloc] initWithFrame:CGRectZero]; 
    myBackgroundView.backgroundColor = [UIColor clearColor]; 
    cell.editingAccessoryView = myBackgroundView; 

任何想法?

+0

你有沒有找到解決這個問題的方法?我有同樣的問題:) – 2012-02-07 13:20:37

回答

7

你快到了。 UITableViewCell類具有backgroundView屬性,默認情況下爲nil。就像您在問題中所做的那樣創建一個新的UIView,然後將其分配給您的單元格的backgroundView屬性。

cell.backgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease]; 
cell.backgroundView.backgroundColor = [UIColor clearColor]; 
1

我認爲這取決於您如何將內容添加到您的單元格。

當我直接使用[cell addSubView]或[cell.contentView addSubView]將內容添加到單元格時,我遇到了同樣的問題。

我的解決方法,這就是:

Create a view 
Add all your content(labels, images etc;) to this view 
Finally then add the view to your cell using [cell addSubView:tmpView] 

而且你不再需要用backgroundView財產篡改。我已經嘗試過,並且完美無缺!

7

我在回答這個問題,因爲我花了一段時間才找到答案,這是搜索中出現的第一個條目。通過使用willDisplayCell方法,您可以訪問單元格背景顏色。請注意,[UIColor clearColor];將返回白色,請相應地調整您的代碼。

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 

    cell.backgroundColor = [UIColor blackColor]; 

} 
0

雖然這些答案都是對的,我覺得對於大多數情況下,它更容易只是設置在界面生成器中的單元格背景顏色。我的意思是細胞的實際背景顏色屬性,而不是它的內容視圖。無論如何,如果它始終是內容視圖的顏色,就沒有理由動態執行此操作。