2012-04-04 73 views
2

我想设置2个完全不同的外观(和内容)的单元格。 (一个选中,一个未选中)。UITableView单元格:selectedBackgroundView而不显示backgroundView

此代码有效,但是,因为选定单元格的图像是50%透明的,我可以在底部看到BackgroundView。

很明显,selectedBackgroundView提供了一种很好的方式来填充单元格,并且元素仅在选中时可见。难道还有比BackgroundView来填充单元的另一个位置,位置怎么也不会出现在selectedBackgroundView

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *cellIdentifier = @"hello"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier]; 
    } 


    UIImage *image = [UIImage imageNamed:@"listview_btn.png"]; 
    UIImage *imageThumb = [UIImage imageNamed:@"01_thumb.jpg"]; 
    UIImageView* imageView = [[UIImageView alloc] initWithImage:imageThumb]; 
    [imageView setFrame:CGRectMake(0, 0, 50, 67)]; 
    [cell setBackgroundView:[[UIImageView alloc]initWithImage:image] ]; 
    [cell.backgroundView addSubview:imageView]; 



    UIImage *imageSel = [UIImage imageNamed:@"listview_btn_on.png"]; 
    UIImage *imageThumbSel = [UIImage imageNamed:@"01_thumb_Sel.jpg"]; 
    UIImageView* imageViewSel = [[UIImageView alloc] initWithImage:imageThumbSel]; 
    [imageViewSel setFrame:CGRectMake(110, 0, 50, 67)]; 
    [cell setSelectedBackgroundView:[[UIImageView alloc]initWithImage:imageSel] ]; 
    [cell.selectedBackgroundView addSubview:imageViewSel]; 
+0

我花了一次点击来修复你的代码格式。也许你应该在下次点击。为此,请选择您插入到问题中的所有代码,然后按下“{}”按钮。它会添加适当的降价缩减,所以你的代码是可读的,并获得很好的配色方案:-) – 2012-04-04 02:55:21

+1

当然,它是[Xcode](http://stackoverflow.com/a/5725282/457406)而不是xCode。 Xcode不是iPhone。 Xcode只是一个IDE,它是编写代码的工具。如果你的问题不是关于IDE本身的(例如“如何在Xcode中打开行号?”,请不要使用标签 – 2012-04-04 02:59:09

+0

这个问题没有答案?(感谢Matthias的更正) – Franck 2012-04-07 12:57:32

回答

0

请选择背景图不透明。由于它总是插在背景视图上方,因此可以将它们组合到Photoshop或其他图形编辑器中,并将结果用作选定的背景视图。

如果您不能负担这种做法(我还是极力推荐它),你也可以继承UITableViewCell,并同时覆盖setHighlighted:animatedsetSelected:animated选择/高亮时背景视图属性设置为无,然后再取消/未加亮时恢复它。但是,这是一个更为复杂的方法,恕我直言。

相关问题