Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the height BETWEEN sections in GROUPED UITableView?

Tags:

ios

iphone

The default height is too large for me. How can I change the height between two sections?

============================================================================

Sorry for my poor English... I mean the gap between two sections is too large. How to change it?

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    CGRect frame = CGRectMake(0, 0, tableView.frame.size.width, 2.0f);
    UIView *view = [[[UIView alloc] initWithFrame:frame] autorelease];
    view.backgroundColor = [UIColor blueColor];
    return view;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    CGRect frame = CGRectMake(0, 0, tableView.frame.size.width, 2.0f);
    UIView *view = [[[UIView alloc] initWithFrame:frame] autorelease];
    view.backgroundColor = [UIColor redColor];
    return view;
}

I changed the header view and footer view of each section. And here is the result: enter image description here

I think there may be a min height of the gap, because I set the height of header and footer to 2px, but the gap between two sections is still so large.

like image 457
理想评论学派 Avatar asked Mar 22 '12 13:03

理想评论学派


2 Answers

You could use footer views between each of your sections and give them the size of the space you desire by implementing the following methods in your UITableView's delegate.

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section

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

In the first delegate method you can return a simple UIView instance, and in the second you can return the height that this view should be. This second delegate method should allow you to control the gap between sections.

like image 94
James Bedford Avatar answered Oct 23 '22 19:10

James Bedford


On iOS 15 you can change the section header top padding

if #available(iOS 15.0, *) {
    tableView.sectionHeaderTopPadding = 0
}
like image 7
rockdaswift Avatar answered Oct 23 '22 20:10

rockdaswift