2009-10-01 80 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]]; 
}