- (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的但无论帧我让它总是显示我相同的红色框。你看到的任何问题,我的代码?

图像:

“在这里输入的图像描述”

更新:

MHM现在我的红色块较高,但我的第一的tableHeader现在以某种方式隐藏。第一个是与titleForHeaderInSection实现。我想我只需要实现的tableHeader高度的高度,但不工作

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

在你的情况,你可以简单地return 30;


此外,您正在泄漏view

[view release]return后会发生。但是,一旦发生return执行被中止的方法和你的release永远不会被调用。

所以,你想这不是

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

和摆脱向下跌破明确release的。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top