UITableView单元格:selectedBackgroundView,不显示backgroundView [英] UITableView cell: selectedBackgroundView without showing backgroundView

查看:1522
本文介绍了UITableView单元格:selectedBackgroundView,不显示backgroundView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to setup 2 completely different look(and content) of a cell. (one selected, and one not selected).

这段代码工作,但是,因为所选单元格的图像是50%透明的,我可以看到底部的BackgroundView 。

This code works, BUT, because the image for the selected cell was 50% transparent, I can see the BackgroundView on bottom.

很清楚,selectedBackgroundView提供了一个很好的方法来填充一个单元格,只有在选中时可见。是否有另一个位置比BackgroundView填充单元格,一个位置如何不会出现在selectedBackgroundView

In clear, selectedBackgroundView offer a nice way to populate a cell with element only visible when selected. Is there another location than BackgroundView to populate cell, a location how will not appear on 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];


推荐答案

由于它总是插入背景视图之上,您可以在Photoshop或其他图形编辑器中合成它们,并将结果用作所选的背景视图。

Make the selected background view opaque. Since it is always inserted above the background view, you can composite them in Photoshop or another graphics editor and use the result as the selected background view.

如果你买不起方法(我仍然强烈推荐它),你可以子类 UITableViewCell 并覆盖 setHighlighted:animated setSelected:animated 将背景视图属性设置为nil(选中/高亮显示),并在取消选择/取消高亮时恢复。然而,它是一个更加复杂的方法,IMHO。

If you cannot afford this approach (I still highly recommend it), you can subclass UITableViewCell and override both setHighlighted:animated and setSelected:animated to set the background view property to nil when selected/highlighted and restore it when deselected/unhighlighted. However it is a much more involved approach, IMHO.

这篇关于UITableView单元格:selectedBackgroundView,不显示backgroundView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆