Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone UITableView : How to remove the spacing between sections in group style table?

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.

like image 664
Bharat Jagtap Avatar asked Feb 14 '14 05:02

Bharat Jagtap


People also ask

Why is there extra padding at the top of my UITableView?

Short answer is that this extra padding is probably due to the table view header (not the section header), and that UITableView doesn't like to be assigned a header with a height of 0.0.

How do you add a space between UITableView cells?

The way I achieve adding spacing between cells is to make numberOfSections = "Your array count" and make each section contains only one row. And then define headerView and its height. This works great.

How do I remove spaces between cells in Swift?

You can remove this by setting a property on the table view: self. tableView.


2 Answers

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.

like image 135
RAJA Avatar answered Nov 12 '22 04:11

RAJA


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
}
like image 23
MMiroslav Avatar answered Nov 12 '22 04:11

MMiroslav