18

I am creating a table view in which there are 10 sections, all having a header view but no cells. So, in short, my table view will display 10 header views only; there will be no cells in any section. Now when I do that there is some space between the section's header views. I want to remove that space. Is that possible? Can you provide me with some hint or work around to achieve this?

Here are the data source methods:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 10; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 0; } -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UILabel * label = [[UILabel alloc] init]; label.backgroundColor = [UIColor grayColor]; return label; } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 44.0f; } 

Here is the screenshot of the output: enter image description here

I have a bit of a complex reason why I am doing it like this, that I wont be able to explain through writing. All I want to do is have no spacing in the section's header views. Thank you in advance.

2
  • 2
    You want 10 section headers and no cells? How about just doing a table view with 10 cells that look like section headers? Commented Feb 14, 2014 at 5:56
  • 1
    @maddy I understand using 10 cells would solve it but as I mentioned the reason I am using header views is bit complex that I can not explain . I have created an expandable cell kind of view . Again its not exactly like just expandable but has got so many other constraints . Commented Feb 14, 2014 at 8:07

11 Answers 11

51

Try this..

 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>; } 

Also have table seprator as none.

Sign up to request clarification or add additional context in comments.

1 Comment

Why can't it just be 0?
23

If you return 0 in tableView:heightForFooterInSection: than default value is returned (probably 10.0). It is tricky, but you can use CGFLOAT_MIN instead of 0 to remove footer.

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

Update:

Swift 3:

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

Comments

4

You can do it directly within the interface, you have to set the footer height to 1 like this :

enter image description here

Comments

3

Try with following code

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return 0.25; // as for you need } 

you can also manage by following delegate method

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

Is short you can manage it by above two method.

2 Comments

Interesting. I just tried and implementing the heightForFooterInSection does have some impact on the spacing between group sections but you can't remove the space completely.
@iPatel actually if you return 0 from - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section then it still creates the footer space with default height , so instead of 0 you will have to return 0.01 , thats a negligible height footer . This works .
3

The grouped style tableview has a default footer, you should also custom the footer to overwrite it

or

try the plain style.

typedef enum { UITableViewStylePlain, UITableViewStyleGrouped } UITableViewStyle; 

Comments

1

Change the Table View style from Grouped to Plain. This solved my problem.

Comments

1

So I can not up vote but I can post a reply

The correct answer to this way in fact

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

Comments

0

You need to use method heightForHeaderInSection . 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.

Comments

0

Swift 5

if you use tableview with .grouped style, maybe you should set header delegate method like this:

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

Comments

0

Swift 5+

override func viewDidLoad() { super.viewDidLoad() if #available(iOS 15.0, *) { self.tableView.sectionHeaderTopPadding = 0 } else { // it will fix from properties of table view } } func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { if section == 0 { return 0; } else { return 50 } } 

enter image description here

Comments

-1

Try using this :

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 0; } 

As mentioned in comments, try using this :

 self.tableView.rowHeight = 0; 

1 Comment

Instead of this just call self.tableView.rowHeight = 0;.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.