将大于cellHeight的子视图添加到UITableViewCell? [英] Adding a subview larger than cellHeight to a UITableViewCell?

查看:98
本文介绍了将大于cellHeight的子视图添加到UITableViewCell?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将子视图添加到UITableViewCell,而我正在使用的设计要求此特定子视图(图像)需要比实际的UITableViewCell更大,因此部分重叠其兄弟。

I'm trying to add a subview to a UITableViewCell and the design that I'm working from demands that this particular subview (an image) needs to be larger than the actual UITableViewCell and thus partly overlap its siblings.

所以我设置了我的表格单元格,生成了我的图像并将其添加到单元格的contentView中:

So I've set up my table cell, generated my image and added it to the cell's contentView:

// rowHeight for the UITableView is 45.0f

UIImage *image = [self createCellThumbnail: someImage];
UIImageView *thumbView = [[UIImageView alloc] initWithFrame: CGRectMake(150, -5, 55,55)];
thumbView.transform = CGAffineTransformMakeRotation(0.1f);
thumbView.image = image;

cell.clipsToBounds = NO;
cell.contentView.clipsToBounds = NO;

[cell.contentView addSubview: thumbView];

当图像溢出进入其下方的单元格时,图像的顶部始终被剪裁,如下所示:

While the image will 'overflow' into the cell below it, the top of the image is always clipped, as demonstrated here:

有谁知道我是不是我使用当前的方法可以做到这一点吗?

Does anyone know if what I'm trying to do is possible with the current approach?

或者我应该想办法在绘制完所有单元格后将这些图像绘制到UITableView上(它是一个不可滚动的tableview,这样就可以了,并且非常简单。)

Or should I just figure out a way to draw these images onto the UITableView after all the cells are drawn (it's a non-scrollable tableview, so that would work and be fairly easy).

更新:

还尝试添加以下内容,但无济于事:

Have also tried adding the following, to no avail:

cell.opaque = NO;
cell.contentView.opaque = NO;

cell.clearsContextBeforeDrawing = NO;
cell.contentView.clearsContextBeforeDrawing = NO;

cell.clipsToBounds = NO;    
cell.contentView.clipsToBounds = NO;


推荐答案

我似乎将tableView从底部呈现为顶部,因此一个细胞上方的细胞与一个细胞重叠。为避免这种情况,您必须将所有单元格的 backgroundColor 设置为 + [UIColor clearColor] 以便您不会看到那些重叠问题。

I seems that the tableView renders its cell from bottom to top, so the cells above one cell overlap that one cell. To avoid this, you'd have to set the backgroundColor of all cells to +[UIColor clearColor] so that you won't see those overlap problems.

但设置 backgroundColor 以清除 -tableView:cellForRowAtIndexPath:没有任何意义。 UIKit在绘制之前会对单元格做很多事情,所以它会重置单元格的 backgroundColor 属性。

But setting the backgroundColor to clear in -tableView:cellForRowAtIndexPath: does not make any sense. UIKit does a lot of stuff with the cell before it's drawn, so does it reset the backgroundColor property of the cell.

我们需要做的是在以后的状态下设置 backgroundColor 。幸运的是有这个 - [UITableViewDelegate tableView:willDisplayCell:forRowAtIndexPath:] 我们可以这样实现:

What we need to do is setting the backgroundColor in a later state. Luckily there is this -[UITableViewDelegate tableView:willDisplayCell:forRowAtIndexPath:] which we can implement like this:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    cell.backgroundColor = [UIColor clearColor];
}

现在我们设置 backgroundColor 在绘制单元格之前,结果证明它正常工作。

Now we're setting the backgroundColor just before the cell is drawn an this turns out to be working.

这篇关于将大于cellHeight的子视图添加到UITableViewCell?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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