viewForHeaderInSection的框架总是大小相同 [英] Frame of viewForHeaderInSection is always the same size

查看:106
本文介绍了viewForHeaderInSection的框架总是大小相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 if(section != 0) {

  UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(10, 10, 100, 30)] autorelease];
  view.backgroundColor = [UIColor redColor];

  return view;

 } else {
  return tableView.tableHeaderView;
 }

}

这是我对viewForHeaderInSection的实现,但无论我做什么帧,它总是向我显示相同的红框。你看到我的代码有什么问题吗?

This is my implementation of viewForHeaderInSection but whatever frame I make it's always showing me the same red frame. Do you see any problem with my code?

图片:

更新:

Mhm现在我的红色区块更高但是我的第一个tableHeader现在以某种方式被隐藏了。第一个是使用titleForHeaderInSection实现的。我以为我只是实现了tableHeader高度的高度但是没有工作

Mhm now my red block is higher but my first tableHeader is now somehow hidden. The first one was implemented with the titleForHeaderInSection. I thought I just implement the height of the tableHeader height but that doesnt work

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if(section == 1)
    return 30;
else
    return tableView.tableHeaderView.frame.size.height;
}


推荐答案

您需要实现此委托方法

    - (CGFloat)tableView:(UITableView *)tableView
heightForHeaderInSection:(NSInteger)section;

在您的情况下,您只需返回30;

In your case, you can simply return 30;.

此外,您正在泄漏查看

Also, you are leaking view!

您的 [查看发布] 返回后发生。但是一旦返回发生,方法执行就会中止,并且永远不会调用 release

Your [view release] happens after the return. But as soon as the return happens the method execution is aborted and your release is never called.

所以你想要这个

UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(10, 10, 100, 30)] autorelease];

并摆脱显式发布下面。

这篇关于viewForHeaderInSection的框架总是大小相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆