Skip to content

Instantly share code, notes, and snippets.

@grinich
Created December 13, 2010 19:44
Show Gist options
  • Save grinich/739485 to your computer and use it in GitHub Desktop.
Save grinich/739485 to your computer and use it in GitHub Desktop.
Custom table header for grouped TTTableViews
#import <Foundation/Foundation.h>
@interface MyTableViewDelegate : TTTableViewGroupedVarHeightDelegate {
}
@end
#import "MyTableViewDelegate.h"
@implementation MyTableViewDelegate
#define SectionHeaderHeight 40
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if ([self tableView:tableView titleForHeaderInSection:section] != nil) {
return SectionHeaderHeight;
}
else {
// If no section header title, no section header needed
return 0;
}
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
if (sectionTitle.length == 0) {
return nil;
}
if (sectionTitle == nil) {
return nil;
}
// Create label with section title
UILabel *label = [[[UILabel alloc] init] autorelease];
label.frame = CGRectMake(20, 6, 300, 30);
// Add a custom style
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.shadowColor = [UIColor grayColor];
label.shadowOffset = CGSizeMake(0.0, 1.0);
label.font = [UIFont boldSystemFontOfSize:16];
label.text = sectionTitle;
// Create header view and add label as a subview
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, SectionHeaderHeight)];
[view autorelease];
[view addSubview:label];
return view;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return[tableView.dataSource tableView:tableView titleForHeaderInSection:section];
}
@end
@grinich
Copy link
Author

grinich commented Dec 13, 2010

In your @TTTableViewController@ subclass, simply add the following to override the default delegate:

- (id<UITableViewDelegate>)createDelegate {
    return [[[MyTableViewDelegate alloc] initWithController:self] autorelease];
 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment