UITableView 单元格:selectedBackgroundView 不显示 backgroundView

Posted

技术标签:

【中文标题】UITableView 单元格:selectedBackgroundView 不显示 backgroundView【英文标题】:UITableView cell: selectedBackgroundView without showing backgroundView 【发布时间】:2012-04-04 00:58:49 【问题描述】:

我想设置一个单元格的 2 个完全不同的外观(和内容)。 (一个被选中,一个未被选中)。

此代码有效,但是,因为所选单元格的图像是 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];

【问题讨论】:

我只需单击一下即可修复您的代码格式。也许你应该下次点击。为此,请选择您插入问题的所有代码,然后按 按钮。它将添加适当的降价缩进,以便您的代码可读并获得漂亮的配色方案:-) 当然是Xcode 而不是xCode。 Xcode 不是 iPhone。 Xcode 只是一个 IDE,它是一个编写代码的工具。如果您的问题与 IDE 本身无关(例如“如何在 Xcode 中打开行号?”,请不要使用标签。 这个问题没有答案? (感谢马蒂亚斯的更正) 【参考方案1】:

使选定的背景视图不透明。由于它始终插入背景视图上方,因此您可以在 Photoshop 或其他图形编辑器中合成它们并将结果用作选定的背景视图。

如果您负担不起这种方法(我仍然强烈推荐它),您可以继承 UITableViewCell 并覆盖 setHighlighted:animatedsetSelected:animated 以在选择/突出显示时将背景视图属性设置为 nil 并在取消选择时恢复它/未突出显示。但是,恕我直言,这是一种更复杂的方法。

【讨论】:

以上是关于UITableView 单元格:selectedBackgroundView 不显示 backgroundView的主要内容,如果未能解决你的问题,请参考以下文章

UItableview 单元格的动态高度与 UItableview 单元格内的 UItableview

UITableView - 如何识别我是不是将一个单元格拖到一个单元格上?

UITableView 单元格未显示放置在自定义单元格上的 UITableView(KPDropDown)

隐藏 UITableview 单元格

UITableView:突出显示最后一个单元格,但其他单元格也会突出显示

从 UITableView 隐藏单元格