本文介绍了有没有一种方法可以自定义UITableViewCell的backgroundView的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将自定义backgroundView分配给UITableViewCell.该表已分组.

I'm trying to assign a custom backgroundView to a UITableViewCell. The table is Grouped.

我有点开拓者,所以我没有使用背景视图的典型宽度302像素,而是尝试使用300 px宽的图像.

I'm a bit of a trailblazer so instead of using the typical width of a background view, 302 pixels, I'm trying to use a 300 px wide image.

这是我的代码:

- (void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    // a 300 pixel wide image
    UIImage *backgroundImage = [UIImage imageNamed:@"tableViewCellBackgroundTop"];
    UIImageView *theBackgroundView = [[UIImageView alloc] initWithImage:backgroundImage];
    theBackgroundView.frame = CGRectMake(10, 0, 300, 49);
    cell.backgroundView = theBackgroundView;
}

如果我在模拟器中运行此程序并使用xScope进行测量,则该单元格看起来仍然是302像素宽.是什么赋予了?我是否需要设置其他属性以使框架尊重分配给背景视图的框架,还是不打算以这种方式自定义表格视图单元格?

If I run this in the simulator and measure with xScope, the cell still appears to be 302 pixels wide. What gives? Is there some other property I need to set to make the framework respect the frame I've assigned to the background view, or were table view cells not intended to be customized in this fashion?

推荐答案

您可以创建一个 UIView 实例,并将 theBackgroundView 为其子视图,然后设置 cell.backgroundView 到创建的视图:

You can create a UIView instance and make theBackgroundView a subview of it, and then set cell.backgroundView to the created view:

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10, 0, 300, 49)];
[view addSubview:theBackgroundView];
cell.backgroundView = view;

这篇关于有没有一种方法可以自定义UITableViewCell的backgroundView的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 14:36