【原】iOS学习之tableView的常见BUG

1、TableView头视图不随视图移动,头视图出现错位

  错误原因:tableView的 UITableViewStyle 没有明确的声明
  解决方法:在tableView声明的时候明确为 UITableViewStyleGrouped

2、分组表视图顶部空白高度调整

enter image description here

 实现方式:

  方式一(推荐使用):

    Swift:

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    if section == 0 {
        return CGFloat.min
    }
    return tableView.sectionHeaderHeight
}

    Obj-C:

 - (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0)
        return CGFLOAT_MIN;
    return tableView.sectionHeaderHeight;
}
  
  方式二:

     In the loadView

_tableView.sectionHeaderHeight = 0;

    Then 

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

  方式三: 

- (void)viewWillAppear:(BOOL)animated{
    CGRect frame = self.tableView.tableHeaderView.frame;
    frame.size.height = 1;
    UIView *headerView = [[UIView alloc] initWithFrame:frame];
    [self.tableView setTableHeaderView:headerView];
}
原文地址:https://www.cnblogs.com/gfxxbk/p/5772183.html