2012-08-02 53 views
0

我在一个视图中有2张桌子。表A列出了一群用户。表B列出了一个用户对象。在表A中选择一行时,表B将重新加载属于该用户的对象。iOS - UITableViewCell的图像在选择时会显示在桌面上?

因此,当用户选择表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

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

在你的UITableViewController子类:

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

    self.view.clipsToBounds = NO; 
} 

这样做有一个副作用,在那里你会看到在的tableview而不是将它们部分地滚动到视图的底部或顶部创建的完整细胞。将表视图放入另一个视图中,将clipToBounds设置为YES,将表视图的边缘与屏幕边缘对齐,或者将视图覆盖底部和顶部(如通常的UIToolbar和UINavigationBar)。

要让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); 
} 

祝你好运。

0

我会建议修改未选中单元格的背景,使得图像资源它与所选背景的宽度相同,但侧面只有一个透明的矩形。