【问题标题】:UITableViewCell subclassed, when selected disappears UIImageViewUITableViewCell 子类化,选中时 UIImageView 消失
【发布时间】:2013-08-30 11:54:52
【问题描述】:

我有一个子类 UITableViewCell,工作正常,当被选中时,它具有自定义单元格颜色和文本颜色,并保持选中状态,直到选择其他单元格。

问题是,我在这个单元格中有一个 UIImageView,当没有选择单元格时它会显示,但是如果选择了单元格,UIImageView 就会消失,

这是我的代码,

#import "MMSideDrawerTableViewCell.h"

@implementation MMSideDrawerTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {


        UIView * backgroundView = [[UIView alloc] initWithFrame:self.bounds];
        [backgroundView setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];


        [backgroundView setBackgroundColor:[UIColor colorWithRed:77.0/255.0
                                                           green:79.0/255.0
                                                            blue:80.0/255.0
                                                           alpha:1.0]];

        [self setBackgroundView:backgroundView];

        UIView *sa = [[UIView alloc]initWithFrame:CGRectMake(180, 4, 20, 20)];
        sa.backgroundColor = [UIColor greenColor];
        [self.contentView addSubview:sa];

        [self.textLabel setTextColor:[UIColor
                                      colorWithRed:230.0/255.0
                                      green:236.0/255.0
                                      blue:242.0/255.0
                                      alpha:1.0]];
        [self.textLabel setShadowColor:[[UIColor blackColor] colorWithAlphaComponent:.5]];
        [self.textLabel setShadowOffset:CGSizeMake(0, 1)];
        [self.textLabel setFont:[UIFont boldSystemFontOfSize:16.0]];



        //highlited
         UIImageView *selectionColor = [[UIImageView alloc] init];
        selectionColor.backgroundColor = [UIColor blackColor];
        self.selectedBackgroundView = selectionColor;

        self.textLabel.highlightedTextColor = [UIColor colorWithRed:49/255.0f green:101/255.0f blue:135/255.0f alpha:1];

     }
    return self; 
} 


@end

有什么问题? 如何让我的图像与选定的单元格一起显示?

编辑, 如果我评论背景选择状态的行,它仍然隐藏图标 uiImageView

    //self.selectedBackgroundView = selectionColor;

谢谢!

【问题讨论】:

  • 我复制并粘贴了您的代码,但看不到您的身份。当未选择单元格时我看不到图像视图,而当它被选中时我会看到,这正是您应该看到的(如果您将图像视图的背景颜色设为红色,则更容易判断)。
  • 嗨@rdelmar 我想在选择和取消选择时查看图像,谢谢
  • 什么图片?您是在谈论 selectionColor 吗?里面有图片吗?
  • 嗨,是的,我用作选定状态背景的 UIImageView,即使我注释了这个背景颜色,它仍然不显示 uiimageV,谢谢
  • 当单元格未被选中时,为什么您希望看到您设置为 selectedBackgroundView 的图像视图?如果您想在选择或取消选择单元格时查看图像视图,只需将其添加为子视图即可。

标签: ios uitableview


【解决方案1】:

您的自定义视图看起来只是一个彩色的UIView。 When a UITableViewcell is selected, all subviews inside of it get their background color changed on highlight.您需要的不仅仅是带有设置背景颜色的UIView。你需要UIImageView,或者类似的东西。

【讨论】:

  • 嗨,谢谢,我也试过了,我已经评论了选定状态的背景但同样的问题,我已经编辑了我的问题欢呼
  • 您是否尝试过使用纯色图像作为自定义视图背景,而不仅仅是一种颜色?甚至使用这样的东西? self.customView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"green.png"]];
  • 我尝试了您的第二个建议,但它不起作用,但在图像视图中使用纯色图像确实有效。 +1
  • 很高兴知道其他选项也不起作用。很奇怪的行为。不过,我很高兴图像选项有效。
  • 您是否有关于单元格子视图在选择时更改其背景颜色的信息的参考?我在任何地方都没有看到。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-08
  • 2011-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多