我正在尝试向 UITableViewCell 添加 subview ,而我正在使用的设计要求此特定 subview (图像)需要大于实际的 UITableViewCell,从而部分重叠其同级 View 。
因此,我设置了表格单元格,生成了图像并将其添加到单元格的 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];
虽然图像会“溢出”到其下方的单元格中,但图像的顶部始终会被剪裁,如下所示:
有谁知道我正在尝试做的事情是否可以通过当前的方法实现?
或者我应该想出一种方法,在绘制所有单元格后将这些图像绘制到 UITableView 上(它是一个不可滚动的表格 View ,这样就可以工作并且相当容易)。
更新:
还尝试添加以下内容,但无济于事:
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]
,这样您就不会看到那些重叠问题。
但是在 -tableView:cellForRowAtIndexPath:
中将 backgroundColor
设置为清除没有任何意义。 UIKit 在绘制单元格之前对单元格执行了很多操作,因此它会重置单元格的 backgroundColor
属性。
我们需要做的是在稍后的状态中设置backgroundColor
。幸运的是,我们可以这样实现 -[UITableViewDelegate tableView:willDisplayCell:forRowAtIndexPath:]
:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [UIColor clearColor];
}
现在我们在绘制单元格之前设置backgroundColor
,结果证明这是有效的。
关于iphone - 将大于 cellHeight 的 subview 添加到 UITableViewCell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2842964/