有没有办法自定义UITableViewCell的backgroundView的大小?

我想分配一个自定义backgroundView到一个UITableViewCell。 表格是分组的。

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

这是我的代码:

- (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像素宽。 是什么赋予了? 是否有一些其他的属性,我需要设置使框架尊重我已经分配给背景视图的框架,或者是表视图单元格不打算以这种方式定制?

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

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