i have a UITableView, that displays expenses from a current month (see screenshot):
My problem is with the header for empty sections. is there any way to hide them? The data is loaded from coredata.
this is the code that generates the header title:
TitleForHeader
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ if ([tableView.dataSource tableView:tableView numberOfRowsInSection:section] == 0) { return nil; } else { NSDate *today = [NSDate date ]; int todayInt = [dataHandler getDayNumber:today].intValue; NSDate *date = [NSDate dateWithTimeIntervalSinceNow:(-(todayInt-section-1)*60*60*24)]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:[[NSLocale preferredLanguages] objectAtIndex:0]]]; [dateFormatter setTimeStyle:NSDateFormatterNoStyle]; [dateFormatter setDateStyle:NSDateFormatterMediumStyle]; NSString *formattedDateString = [dateFormatter stringFromDate:date]; return formattedDateString;} }
ViewForHeader
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ if ([tableView.dataSource tableView:tableView numberOfRowsInSection:section] == 0) { return nil; } else { UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 312, 30)]; UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(4, 9, 312, 20)]; UIView *top = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 312, 5)]; UIView *bottom = [[UIView alloc]initWithFrame:CGRectMake(0, 5, 312, 1)]; [top setBackgroundColor:[UIColor lightGrayColor]]; [bottom setBackgroundColor:[UIColor lightGrayColor]]; [title setText:[expenseTable.dataSource tableView:tableView titleForHeaderInSection:section]]; [title setTextColor:[UIColor darkGrayColor]]; UIFont *fontName = [UIFont fontWithName:@"Cochin-Bold" size:15.0]; [title setFont:fontName]; [headerView addSubview:title]; [headerView addSubview:top]; [headerView addSubview:bottom]; return headerView; } }
heightForHeader
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { NSLog(@"Height: %d",[tableView.dataSource tableView:tableView numberOfRowsInSection:section] == 0); if ([tableView.dataSource tableView:tableView numberOfRowsInSection:section == 0]) { return 0; } else { return 30; } }
numberOfRowsInSection
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { int rows = 0; for (Expense* exp in [dataHandler allMonthExpenses]) { if ([exp day].intValue == section) { rows++; } } return rows; }
sebastian
I also wanted to hide the headerView. To do this, I did the following: Set up your tableView as per the excellent answer @sasquatch gave above. In the numberOfRowsInSection(section: Int) and tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) functions, check whether the rows/height should be 0.
Hiding a row/cell is super simple. All we have to do is set the row/cell height. Luckily for us, UITableViewController already has this method built in. For demonstration purposes I have just set the height where indexPath.
To create a basic header or footer with a text label, override the tableView:titleForHeaderInSection: or tableView:titleForFooterInSection: method of your table's data source object. The table view creates a standard header or footer for you and inserts it into the table at the specified location.
You have to set tableView:heightForHeaderInSection:
to 0 for the appropriate sections. This is something which changed fairly recently and got me in a couple places. From UITableViewDelegate
it says...
Prior to iOS 5.0, table views would automatically resize the heights of headers to 0 for sections where tableView:viewForHeaderInSection: returned a nil view. In iOS 5.0 and later, you must return the actual height for each section header in this method.
So you'll have to do something like
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { if ([tableView.dataSource tableView:tableView numberOfRowsInSection:section] == 0) { return 0; } else { // whatever height you'd want for a real section header } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With