解决iOS UITableView分组header悬浮,每个section header上面有一段空白间距

如上图,按照以前的添加方式header上面会有空白。

最新方法设置headerTop间距为0,就正常了

self.tableView.sectionHeaderTopPadding = 0;

添加headerView方式还是一样的只需设置一下sectionHeaderTopPadding高度就可以了

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 5;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return  5;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableViewCell" forIndexPath:indexPath];

    cell.backgroundColor = [UIColor redColor];

    return  cell;

}

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

    return 20.0f;

}

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

    UILabel *view = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];

    view.backgroundColor = [UIColor grayColor];

    view.text = @"sectionHeader";

    return  view;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return  44.0f;

}

猜你喜欢

转载自blog.csdn.net/smile82475/article/details/125187582