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.

Community
  • 1
  • 1
Bharat Jagtap
  • 1,692
  • 2
  • 22
  • 35
  • 2
    You want 10 section headers and ***no*** cells? How about just doing a table view with 10 cells that look like section headers? – Michael Dautermann Feb 14 '14 at 05:56
  • 1
    I agree with michael you should only use cell instead of headers – rahul Feb 14 '14 at 05:58
  • 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 . – Bharat Jagtap Feb 14 '14 at 08:07

10 Answers10

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.

RAJA
  • 1,214
  • 10
  • 13
  • 3
    Yes it is working , I just returned 0.01f from heightForFooterInSection method and it worked . Thank You so much . If you could just edit the answer and makes it may be 0.01 ,I can mark it as the right answer . – Bharat Jagtap Feb 14 '14 at 08:19
  • @RAJA- Thank You very much. It is really a smart answer and I just didn't look on that. – Mohd Haider Jun 05 '14 at 07:35
  • 1
    Why can't it just be 0? – Carlos Sep 19 '15 at 19:41
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
}
MMiroslav
  • 1,672
  • 20
  • 32
4

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

enter image description here

Ugo Marinelli
  • 989
  • 1
  • 11
  • 18
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.

iPatel
  • 46,010
  • 16
  • 115
  • 137
  • There are no footers so using that delegate won't change anything. – rmaddy Feb 14 '14 at 06:03
  • I tried with `heightForFooterInSection` and it working for me in grouped tableView – iPatel Feb 14 '14 at 06:05
  • Have you also implemented either `titleForFooterInSection` or `viewForFooterInSection`? `heightForFooterInSection` should only have an affect if either of those other two are also implemented. – rmaddy Feb 14 '14 at 06:07
  • @rmaddy - NO, in my project I does not use both method *(either titleForFooterInSection or viewForFooterInSection)* .. then also work for me. – iPatel Feb 14 '14 at 06:10
  • 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. – rmaddy Feb 14 '14 at 06:19
  • @rmaddy - you completely remove the space between two section by using `- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section` set `return 0;`.. :) – iPatel Feb 14 '14 at 06:21
  • @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 . – Bharat Jagtap Feb 14 '14 at 11:53
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;
lancy
  • 857
  • 6
  • 22
1

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

user2179059
  • 326
  • 1
  • 9
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;
}
Travis Delly
  • 1,194
  • 2
  • 11
  • 20
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.

Ash
  • 5,525
  • 1
  • 40
  • 34
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()
}
Sigit
  • 51
  • 1
  • 1
  • 4
-1

Try using this :

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

As mentioned in comments, try using this :

 self.tableView.rowHeight = 0;
Samkit Jain
  • 2,523
  • 16
  • 33