【问题标题】:Blank first UITableView section header空白的第一个 UITableView 部分标题
【发布时间】:2013-09-30 22:02:41
【问题描述】:

我的 UITableView 中有 2 个部分。我希望我的第一个标题不存在,没有空间,什么都没有。第一个单元格触摸屏幕顶部。我想要我的第二部分的自定义部分标题。

如果我不使用- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section,我可以这样做,但是当我使用此方法时,第一部分会出现一个空白标题视图。我做错了什么?

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (!section == 0){
        UIView *header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30.0f)];
        UILabel *label = [[UILabel alloc]initWithFrame:[header frame]];
        [label setText:@"Test Header"];
        [header addSubview:label];

        return header;
    }
    else{
        // This apparently causes a header to appear in section 0. 
        // Without this entire method it works fine- but it means I can't customize
        // the header.
        return nil;
    }



}

【问题讨论】:

    标签: objective-c ios uitableview


    【解决方案1】:

    寻找

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

    UITableView的方法。

    第一部分可以return 0;

    【讨论】:

      【解决方案2】:

      使用这个,

      - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
      {
          if (section == 0){       
              return 0;
          }
          //...
      }
      

      【讨论】: