理想评论学派 理想评论学派 - 3 months ago 16
iOS Question

How to change the height BETWEEN sections in GROUPED UITableView?

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.

Answer

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.