理想评论学派
理想评论学派

Reputation: 1001

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.

Upvotes: 14

Views: 23059

Answers (5)

Ash
Ash

Reputation: 5712

Edit:

Instead of CGFLOAT_MIN you should use CGFloat.leastNormalMagnitude


You need to use method heightForHeaderInSection for defining space between header & cell text. You can also change it depending on different sections for eg. at some sections you may need to show more distance & under some, you don't want to show gap. For such case you can use

CGFLOAT_MIN which is 0.000001f

. Giving you an example, how you can use different section with different header heights

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0 || section == 2)
    {
        return 55.0;
    }
    else
    {
        return CGFLOAT_MIN;
    }
}

Hope it will help you.

Upvotes: 0

rockdaswift
rockdaswift

Reputation: 9983

On iOS 15 you can change the section header top padding

if #available(iOS 15.0, *) {
    tableView.sectionHeaderTopPadding = 0
}

Upvotes: 8

algrid
algrid

Reputation: 5964

For those who want to increase the distance.

The solutions based on setting header/footer height can have one side effect: if you have some text for you header or footer, it will be centered vertically inside your increased header/footer.

To solve this you can use a custom header/footer view, but I would suggest adding an empty section where you need additional gap and setting footer height for it (set " " as the footer text, in case of nil or an empty string it won't be displayed).

Upvotes: 0

Carl
Carl

Reputation: 250

try out this code

self.tableView.rowHeight = 0; // in viewdidload
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]; //    in viewdidload

-(CGFloat)tableView:(UITableView *)tableView    heightForFooterInSection:(NSInteger)section
{
    return 0.01f;
}

-(CGFloat)tableView:(UITableView *)tableView    heightForHeaderInSection:(NSInteger)section{
    return <your header height>;
}

- (UIView *)tableView:(UITableView *)tableView  viewForFooterInSection:(NSInteger)section{
    return [[UIView alloc] initWithFrame:CGRectZero];
}

- (UIView *)tableView:(UITableView *)tableView  viewForHeaderInSection:(NSInteger)section{
    return <your header view>;
}

and the Swift version is

func tableView(tableView: UITableView,  viewForFooterInSection section: Int) -> UIView? {
    return your footer view
}

func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {

    return your footer view height
}

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 0.01
}

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    return UIView.init(frame: CGRect.zero)
}

Upvotes: 4

James Bedford
James Bedford

Reputation: 28982

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.

Upvotes: 17

Related Questions