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

时间:2012-08-14 18:01:08

标签: iphone ios uitableview

我正在尝试将自定义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像素宽。是什么赋予了?我需要设置一些其他属性来使框架尊重我分配给背景视图的框架,还是不打算以这种方式自定义的表视图单元格?

1 个答案:

答案 0 :(得分:3)

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

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