Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grouped UITableView shows blank space when section is empty

I have a grouped UITableView where not all sections may be displayed at once, the table is driven by some data that not every record may have. My trouble is that the records that don't have certain sections show up as blank spaces in the table (see picture)

alt text http://img220.imageshack.us/img220/5633/screenshot20100322at228.png

There are no footers/headers. Anything I've missed?

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. return [self getRowCount:section]; }   // Customize the appearance of table view cells. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  static NSString *CellIdentifier = @"Cell";  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) {     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; }  cell.textLabel.text = [NSString stringWithFormat:@"section: %d row: %d",indexPath.section,indexPath.row]; // Configure the cell...  return cell; }  - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {      float height = 0.0;      if([self getRowCount:indexPath.section] != 0){         height = kDefaultRowHeight;     }      return height;  } 
like image 843
christo16 Avatar asked Mar 22 '10 21:03

christo16


People also ask

How do you remove the blank space at the top of a grouped UITableView?

You can change the space between tableviewcells themselves by: [myTableView setSectionHeaderHeight:0]; [myTableView setSectionFooterHeight:25];

What is grouped UITableView?

Style. grouped. A table view where sections have distinct groups of rows.

What is Section in UITableView?

UITableView with sections allows us to separate the list into different categories, so the list looks more organized and readable. We can customize sections as per our need, but in this tutorial, we are covering the basic UITableview with sections. Here's is the video if you prefer video over text. Let Create An App.


2 Answers

I have a similar situation and my data model really works better if I can let it have empty sections.

Your problem can be solved if you set self.tableView.sectionHeaderHeight = 0; and self.tableView.sectionFooterHeight = 0; in your tableview controller. These must be 0 because the delegate methods somehow ignore a return value of 0. Then you have to override some delegate methods:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {     if (section==0)         return sectionGapHeight;     if ([self tableView:tableView numberOfRowsInSection:section]==0) {         return 0;     }     return sectionGapHeight; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {     if (section==[self numberOfSectionsInTableView:tableView]-1) {         return sectionGapHeight;     }     return 0; } - (UIView *)sectionFiller {     static UILabel *emptyLabel = nil;     if (!emptyLabel) {         emptyLabel = [[UILabel alloc] initWithFrame:CGRectZero];         emptyLabel.backgroundColor = [UIColor clearColor];     }     return emptyLabel; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {     return [self sectionFiller]; } - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {     return [self sectionFiller]; } 

This code will also make the gap between sections the same height as the gap before the first section and the gap below the last. That looks better in my opinion than the default where the gap between is twice as big as the ones at the top and the bottom.

like image 93
user362178 Avatar answered Sep 23 '22 16:09

user362178


Here is the solution that worked for me

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {     switch (section) {         case 1: case 3: case 5: return 20.0f; break;       default: return 0.01f; // for some reason 0 is not accepted - give something :)     } }  - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {     switch (section) {         case 1: case 3: case 5: return 20.0f; break;       default: return 0.01f; // for some reason 0 is not accepted - give something :)     } } 
like image 29
Ramesh Avatar answered Sep 23 '22 16:09

Ramesh