在视图控制器中显示带有图像和标签的标题视图

发布于 2024-08-22 03:16:11 字数 99 浏览 3 评论 0原文

我是 iPhone 开发新手。我创建了一个视图控制器,并使用了分组表视图。现在我想在我的视图控制器中显示带有图像和标签的标题视图。请指导我并帮助我解决这个问题。

谢谢。

I am new to iPhone development. I have created one view controller and I used grouped table view. Now I want to display the header view in my viewcontroller with image and labels. Please guide me and help me out in this problem.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青衫儰鉨ミ守葔 2024-08-29 03:16:11

你是指 headerView 还是sectionHeaderView?您可以在 viewDidLoad 方法中将子视图添加到 headerView:

- (void)viewDidLoad {
    [super viewDidLoad];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 225)];
    label.text = @"BlaBla";
    [self.tableHeaderView addSubview:label];
}

您可以使用 initWithFrame 方法指定标签的大小和位置,并将标签作为子视图添加到 tableHeaderView - 您可以用多个标签来做到这一点。

如果您指的是sectionHeader,则必须实现tableView:viewForHeaderInSection:方法,您必须在其中创建一个新视图,并向其添加不同的子视图:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 40)];
    label.text = @"BlaBla";
    [view addSubview:label];
    [label release];

    return [view autorelease];
}

在这种情况下,您还必须实现该方法< code>tableView:heightForHeaderInSection: 必须返回您在上述方法中创建的视图的高度:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50.0f;
}

Do you mean a headerView or a sectionHeaderView? You can add subviews to the headerView in the viewDidLoad method:

- (void)viewDidLoad {
    [super viewDidLoad];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 225)];
    label.text = @"BlaBla";
    [self.tableHeaderView addSubview:label];
}

You specify size and position of the label with the initWithFrame method, and add the label as subview to the tableHeaderView - you can do this with multiple labels.

If you mean the sectionHeader you have to implement the tableView:viewForHeaderInSection: method, where you have to create a new view, and add different subviews to it:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 40)];
    label.text = @"BlaBla";
    [view addSubview:label];
    [label release];

    return [view autorelease];
}

In this case you also have to implement the method tableView:heightForHeaderInSection: which has to return the height of the view you create in the above method:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50.0f;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文