UITableViewCell子视图问题

时间:2012-08-16 10:23:13

标签: ios ipad uitableview

我从xib加载单元格。我在单元格中添加了3个垂直分隔符(尝试从xib和代码中添加)。在我选择单元格之前一切正常。当我选择卖出时,分隔符就会消失。

以下是图片: 未选中的: enter image description here

选择的 enter image description here

添加分隔符的代码:

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.topSeparator = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 1)] autorelease];
        [self addSubview:topSeparator];

        self.bottomSeparator = [[[UIView alloc] initWithFrame:CGRectMake(0, self.bounds.size.height - 1, self.bounds.size.width, 1)] autorelease];
        [self addSubview:bottomSeparator];

        // Initialization code
        CGFloat xOffset = 314;
        UIView *imageView1 = [[[UIView alloc] initWithFrame:CGRectMake(xOffset, 0, 1, self.bounds.size.height)] autorelease];
        imageView1.tag = kTagBorder1;
        imageView1.backgroundColor = [UIColor blackColor];
        [self addSubview:imageView1];

        xOffset = 487;
        imageView1 = [[[UIView alloc] initWithFrame:CGRectMake(xOffset, 0, 1, self.bounds.size.height)] autorelease];
        imageView1.tag = kTagBorder2;
        imageView1.backgroundColor = [UIColor blackColor];
        [self addSubview:imageView1];

        xOffset = 573;
        imageView1 = [[[UIView alloc] initWithFrame:CGRectMake(xOffset, 0, 1, self.bounds.size.height)] autorelease];
        imageView1.tag = kTagBorder3;
        imageView1.backgroundColor = [UIColor blackColor];
        [self addSubview:imageView1];
    }
    return self;
}

我甚至试过这个:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    [self bringSubviewToFront:[self viewWithTag:kTagBorder1]];
    [self bringSubviewToFront:[self viewWithTag:kTagBorder2]];
    [self bringSubviewToFront:[self viewWithTag:kTagBorder3]];
    [self setNeedsDisplay];

    // Configure the view for the selected state
}

2 个答案:

答案 0 :(得分:0)

您不应将这些行添加为selfUITableViewCell)的子视图。而是将其添加为self.contentView的子项 - 这是包含UITableViewCell内部组件的视图。

This article contains tips on how to customize a UITableView properly

答案 1 :(得分:0)

我修好了。我使用黑色图像的UIImageView而不是黑色背景的uiview。在这种情况下,视图不会消失。