开发者

Reducing the space between sections of the UITableView

Is there a way to reduce the space between two sections of a UITableView? There are about 15 pixels between every single section I have. I did already try to return 0 for -tableView:heightForFooterInSection: and -tabl开发者_开发问答eView:heightForHeaderInSection: but that doesn't change anything.

Any suggestions?


It was a bit tricky, but try this code:

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

    return 1.0;
}

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

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

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

Change the values accordingly. To remove the space, I think 0.0 will not be accepted. The smallest sane value seems to be 1.0.


For all who want to shrink the distance to 0 you have to use:

tableView.sectionHeaderHeight = 0.0;
tableView.sectionFooterHeight = 0.0;

Because the serving of the UITableViewDelegate does only make an effect starting from floats greater than zero.

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


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

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

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

(using iOS 4.1 with XCode 4.0.2)


You can actually set the footer/header/cell heights in Interface Builder under the size tab. By default the header/footer are set at 10.0.


You have to reduce the section header/footer height. Then the space between sections will be reduce.

try this code

It works for me :-)

tableView.sectionHeaderHeight = 2.0;
tableView.sectionFooterHeight = 2.0;


You can also reduce the height of section footer and header from the storyboard. In the tableview -> size inspector. Go to Section Height.

Reducing the space between sections of the UITableView

By default it is set to 22 for Plain style table and 10 for grouped style table. You can configure values by increasing / decreasing the values for header and footer separately.


For me the issue was because I was using a grouped table view style (UITableViewStyleGrouped). When I changed it to a plain style (UITableViewStylePlain) my tableView:heightForHeaderInSection: method was the only thing determining the size of each section header.


Use this perhaps, 0 will not work as expected

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

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return .leastNormalMagnitude
}


UPDATE FOR iOS7: Due to ARC autorelease update, here's @Martin Stolz code edited.

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

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

-(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
{
    return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
}

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

(Using iOS7, Xcode v5.0)


For me just this issue got resolved just by using the following code,

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return section == 0 ? 1 : 20 // 20 is my other sections height
}

Returning 1 for the first section has solved the issue.


To change header/footer space the following methods have to be implemented:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?

AND

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat

(use corresponding methods to change footer height)

The following code completely removes spaces around sections:

public func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    return nil
}

public func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    return nil
}

public func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return .leastNonzeroMagnitude
}

public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return .leastNonzeroMagnitude
}


Swift 5, iOS 13+

Option 1 - When creating the table view

tableView.sectionHeaderHeight = 0
tableView.sectionFooterHeight = 0

Option 2 - More flexible for other views and sections

// Top space for sections
public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 0
}

// Bottom space for sections
public func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 0
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜