【问题标题】:Change Header View iOS更改标题视图 iOS
【发布时间】:2012-01-19 01:17:38
【问题描述】:

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

- (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;

}

表格视图标题保持空白。

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

【问题讨论】:

  • 您是否验证了您的图片已正确加载到您的图片视图中?
  • 是的。它之前已加载,但未在标题视图中加载。它加载到单元格上方(它覆盖了第一个单元格并稍微进入第二个单元格。第二个单元格开始的地方有一条白线)。

标签: ios xcode button view uiviewcontroller


【解决方案1】:

这对我来说非常有效:

- (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];
}

【讨论】:

    【解决方案2】:

    参考:Apple Developer UITableViewDelegate

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

    【讨论】:

    • -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 90;仍然什么都没有...如果我只有一个,我是否需要指定该部分?谢谢
    • 没必要。现在,不要添加图像,而是将 customView 的背景颜色设置为某种颜色并检查 customView 是否正确加载。如果是,则使用 320*90 分辨率的图像进行检查。
    • 我让它加载(图片也是),但它没有将它加载到标题中。它将它加载到标题的正下方(通常文本所在的位置仍然可见)。如何删除空白标题(或将我的图片插入其中?)
    • 你能提供你现在得到的截图吗?这是我从您的代码中得到的屏幕截图(插入此行后:i.frame = customView.frame; 就在 [customView addSubview:i]; 之前):mediafire.com/i/?zs83ms0i1bb8let。我想这就是你想要的。
    • 这也是我得到的。我没有使用 UINavigationController,而是使用了 UITableView,并让图片显示在正确的位置。现在我不能让我的细胞出现!我猜一步一步。如果我想不通,我会回帖。谢谢大家
    【解决方案3】:

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

    【讨论】:

    • 还是什么都没有...我输入了你给的代码(i.frame = customView.frame;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-01
    • 2016-12-23
    相关资源
    最近更新 更多