2009-10-01 43 views
8

我使用自定義的UITableViewCell從筆尖。附件視圖是詳細披露指標。問題在於附件視圖後面的UITableViewCell的背景顏色未呈現(請參閱下面的圖像/源代碼)。任何線索?另外,這裏有一些東西,我試過,但沒有奏效:如何獲得UITableViewCell中的透明配件視圖? (W /截圖)

事情沒有工作:
- 設置附屬視圖的backgroundColor至clearColor
- 設置contentView.opaque細胞對FALSE
- 設置表視圖的contentView.opaque爲FALSE
- 設置爲電池


非默認附件視圖

alt text http://www.chicknchoke.com/so/IMG_8028.png

-(void)showTablePrep 
    { 
     myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 480, 320, 416) style:UITableViewStylePlain]; 
     myTableView.dataSource = self; 
     myTableView.delegate = self; 
     myTableView.delaysContentTouches = FALSE; 
     myTableView.opaque = FALSE; 
     myTableView.rowHeight = 60; 

     [UIView beginAnimations:@"SlideUp" context:nil]; 
     [UIView setAnimationDuration:0.3f]; 

     [myTableView setCenter:CGPointMake(myTableView.center.x, myTableView.center.y-436)]; 
     [self.view addSubview:myTableView]; 
     [self.view bringSubviewToFront:myTableView]; 

     [UIView commitAnimations]; 


    } 




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    FriendsCell* cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCellID"]; 

    if (cell == nil){ 

     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"FriendsCellView" owner:self options:nil]; 

     cell = (FriendsCell*)[nib objectAtIndex:0]; 

     if(indexPath.row % 2 == 0){ 

     cell.contentView.backgroundColor = [UIColor grayColor]; 


     }else{ 

     cell.contentView.backgroundColor = [UIColor lightGrayColor]; 


     } 
     cell.accessoryView.backgroundColor = [UIColor clearColor]; 
     cell.contentView.opaque = FALSE; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 


    if(f 

riendsCounter > indexPath.row){ 


      cell.titleLabel.text = @"Label"; 
      cell.descLabel.text = @"Description goes here"; 

     }else{ 

      cell.titleLabel.text = @""; 
      cell.descLabel.text = @""; 
      cell.accessoryType = UITableViewCellAccessoryNone; 

     } 

     } 

     return cell;  
    } 

回答

11

你繪製的背景色你的錯誤。甲UITableViewCell將佈置成使得contentViewaccessoryView坐並排側。 (這樣做是爲了使contentView可以剪切其內容,使其不與附件視圖重疊)問題不在於附件視圖不透明,而是灰色背景根本不在附件視圖後面繪製。

定製UITableViewCell背後繪製的背景的正確方法是自定義其backgroundView。我沒有試過,但是,因爲你只改變顏色,你也許可以簡單地在backgroundViewbackgroundColor顏色設置爲你想要的顏色。

+0

我也想提一提,有一個非常好的定製'UITableView'繪圖的概述:http://cocoawithlove.com/2009/04/easy-custom-uitab leview-drawing.html – Alex 2009-10-01 14:56:47

+7

這對我有用: cell。backgroundView = [[UIView alloc] init]; cell.backgroundView.backgroundColor = [UIColor yellowColor]; – joshaidan 2011-05-02 18:56:09

+1

@joshaidan不要忘記釋放你創建的視圖(除非你使用ARC):cell.backgroundView = [[[UIView alloc] init] autorelease]; – manicaesar 2012-06-18 14:17:11

1

我由具有看看我定製表格視圖單元格的子視圖找到了答案。

好像附屬視圖有一個按鈕坐在了它。通過在子視圖中找到此按鈕並更改顏色,我可以更新附件按鈕後面的背景顏色。

<UIButton: 0x3b4d690; frame = (277 0; 43 75); opaque = NO; layer = <CALayer: 0x3b3e0b0>> 

for (UIView *aSubView in self.subviews) { 
    if ([aSubView isMemberOfClass:[UIButton class]]) { 
     aSubView.backgroundColor = [UIColor greenColor]; 
    } 
} 

不幸的是我只能以我的自定義表視圖細胞類的

- (void)setSelected:(BOOL)selected animated:(BOOL)animated 

方法內達到這個按鈕。我在我的應用程序中成功地使用了這個功能,當用戶選擇一個單元格時顯示不同的高亮顏色。這應該指向你正確的方向。

0

我解決,但是,iOS7這個問題將在

[cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

和修改你的背景和其他bakcgrounds這裏:

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath { 
    return YES; 
} 

- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Add your Colour. 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    [cell setBackgroundColor:[UIColor colorWithHexColor:HIGHLIGHT_COLOR]]; 
} 

- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Reset Colour. 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    [cell setBackgroundColor:[UIColor colorWithHexColor:UNHIGHLIGHT_COLOR]]; 
}