Created
December 13, 2010 19:44
-
-
Save grinich/739485 to your computer and use it in GitHub Desktop.
Custom table header for grouped TTTableViews
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface MyTableViewDelegate : TTTableViewGroupedVarHeightDelegate { | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In your @TTTableViewController@ subclass, simply add the following to override the default delegate: