Frame of viewForHeaderInSection is always the same size

半腔热情 提交于 2020-01-12 04:10:49

问题


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

}

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?

Image:

UPDATE:

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

回答1:


You need to implement this delegate method

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

In your case, you can simply return 30;.


Also, you are leaking view!

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.

So you want this instead

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

And get rid of the explicit release down below.



来源:https://stackoverflow.com/questions/2450122/frame-of-viewforheaderinsection-is-always-the-same-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!