更改标题视图 iOS

发布于 2024-12-20 19:53:00 字数 455 浏览 4 评论 0原文

我四处寻找答案并想出了这段代码。我正在尝试将图片插入标题视图(通常是文字的位置)。

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

{

    UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 90.0)];

    UIImageView *i=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"IMAGE NAME"]];

    [customView addSubview:i];

    return customView;

}

表视图标题保持空白。

有什么想法吗?我刚刚开始...谢谢!

I have looked around for an answer and came up with this code. I am trying to insert a picture into the header view (where words normally would be).

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

{

    UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 90.0)];

    UIImageView *i=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"IMAGE NAME"]];

    [customView addSubview:i];

    return customView;

}

The table view header remains blank.

Any ideas? I'm just starting out here...Thanks!

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

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

发布评论

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

评论(3

你不是我要的菜∠ 2024-12-27 19:53:00

这对我来说非常有效:

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

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIImageView *i = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"IMAGE NAME"]];
    i.frame = CGRectMake(0.0, 0.0, 320.0, 90.0);
    return [customView autorelease];
}

That works perfectly for me:

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

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIImageView *i = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"IMAGE NAME"]];
    i.frame = CGRectMake(0.0, 0.0, 320.0, 90.0);
    return [customView autorelease];
}
花伊自在美 2024-12-27 19:53:00

参考: Apple 开发者 UITableViewDelegate

返回的对象可以是 UILabel 或 UIImageView 对象,以及自定义视图。此方法仅在 tableView:heightForHeaderInSection: 也实现时才能正确工作。
尝试一下。

Reference: Apple Developer UITableViewDelegate

The returned object can be a UILabel or UIImageView object, as well as a custom view. This method only works correctly when tableView:heightForHeaderInSection: is also implemented.
Try that.

安静 2024-12-27 19:53:00

为 UIImageView 设置一个框架,例如:
i.frame = customView.frame;

set a frame for the UIImageView, eg:
i.frame = customView.frame;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文