0

如何更改表格视图标题的宽度?

我有一个 UITableView 和一个标题,它是在 ViewForHeaderInSection 中设置的 UIImageView。

当我运行应用程序时,标题会延伸到屏幕的宽度,而行(也是图像)是我想要的宽度。

高度似乎还可以,只是宽度是问题所在。

当我创建图像以设置为标题时,我为它制作了一个框架,但它没有做任何事情。

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

UIImage *tableHeaderImage = [UIImage imageNamed:@"tableTop"];
UIImageView *headerImageView = [[[UIImageView alloc] initWithImage:tableHeaderImage] autorelease];

headerImageView.frame = CGRectMake(0,0,277,7);
NSLog(@"%@",headerImageView.frame.size.width);    

return headerImageView;
}

ps它都是以编程方式编码的。

4

1 回答 1

1

您需要返回一个UIView. 设置完成后,将图像视图添加为子视图并返回视图:

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(tableView.frame), 7);
    UIImage *tableHeaderImage = [UIImage imageNamed:@"tableTop"];
    UIImageView *headerImageView = [[[UIImageView alloc] initWithImage:tableHeaderImage] autorelease];

    headerImageView.frame = CGRectMake(0,0,277,7);
    [view addSubview headerImageView];
    return view;
}
于 2012-08-10T03:42:32.733 回答