的UITableView细胞:selectedBackgroundView而不显示backgroun

2019-09-16 08:22发布

我想设置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];

Answer 1:

请选择背景图不透明。 因为它总是在后台视图上方插入,你可以复合他们在Photoshop或其他图形编辑器,并使用结果作为选择的背景图。

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



文章来源: UITableView cell: selectedBackgroundView without showing backgroundView