iOS - UITableViewCell图像在选择后会在桌面上显示?

时间:2012-08-02 02:08:52

标签: objective-c ios uitableview

我在一个视图中有2个表。表A列出了一堆用户。表B列出了用户对象。在表A中选择行时,将使用属于该用户的对象重新加载表B.

因此,当用户选择表A中的行时,单元格背景中的图像将更改为图像的突出显示版本。

以下是背景图片的正常版本:default image

以下是背景图片的突出显示版本:enter image description here

如您所见,突出显示的版本右侧有一个小箭头。此箭头超出了表格单元格表格的宽度。选择行后,图像会按原样更改,但图像的大小会缩小以使整个图像适合单元格。

我想要发生的是图像在表格之外,或者在表格顶部的所选行。

我认为一种可能的解决方案是将表格放在所选行的中心,然后覆盖该图像,但如果用户试图滚动表格,那么图像将需要移动,这将是一个巨大的痛苦。

所以我想知道的是,可以将单元格的大小扩展到选择的表格之外吗?

提前致谢!

编辑:

以下不起作用,以防任何人试图尝试:

[cell setFrame:CGRectMake(cell.frame.origin.x, cell.frame.origin.y, cell.frame.size.width+20, cell.frame.size.height)];

1 个答案:

答案 0 :(得分:1)

将视图clipsToBounds属性设置为NO将允许视图在其自己的框架之外绘制。

在你的UITableViewController子类中:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do what you normally do here, if anything then add...

    self.view.clipsToBounds = NO;
}

执行此操作会产生副作用,您会在桌面视图的底部或顶部看到创建完整单元格,而不是部分滚动到视图中。将表视图放入另一个视图,其中clipsToBounds设置为YES,将表视图的边缘与屏幕边缘对齐,或者让视图覆盖底部和顶部(如UIToolbar和UINavigationBar通常那样)。 p>

要使UITableViewCell的selectedBackgroundView扩展到表视图的框架之外,请创建UITableViewCell的子类并覆盖layoutSubviews方法。

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

        self.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"YQGyZ"]];
        self.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"CQXYh"]];

        self.textLabel.backgroundColor = [UIColor clearColor];
        self.detailTextLabel.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];

    CGRect frame = self.selectedBackgroundView.frame;
    frame.size.width += 13; // where 13 is the size of the arrow overhang from the same images
    self.selectedBackgroundView.frame = frame;

    // You can also change the location of the default labels (set their background colors to clear to see the background under them)
    self.textLabel.frame = CGRectMake(70, 0, 148, 30);
    self.detailTextLabel.frame = CGRectMake(70, 30, 148, 30);
}
祝你好运。