I'll need to customize the header section of a UITableViewController
where for each sections a different header text is returned (getting data from datasource as well). This is accomplished using the following:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSArray *temp = [listOfMBeans allKeys];
DLog(@"MBean details: %@", temp);
NSString *title = [temp objectAtIndex:section];
DLog(@"Header Title: %@", title);
return title;
};
This works well and I can see the expected output. However I need to change also the font size of text and after looking at similar questions I've implemented the following:
- (UIView *) tableview:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
DLog(@"Custom Header Section Title being set");
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] autorelease];
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] autorelease];
label.text = [tableView.dataSource tableView:tableView titleForHeaderInSection:section];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:14];
[headerView addSubview:label];
return headerView;
}
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 44.0;
}
However it seems that the code is never called. My understanding was that UITableViewController
is setting by default itself as delegate but it seems I'm wrong.
The UITableViewController
is created in this way (as part of hierarchical data):
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ProjectDetails *detailViewController = [[ProjectDetails alloc] initWithStyle:UITableViewStyleGrouped];
detailViewController.project = [listOfMetrics objectAtIndex:indexPath.row];
// Push the detail view controller.
[[self navigationController] pushViewController:detailViewController animated:YES];
[detailViewController release];
}
What changes, I should make to make this working? Thanks.
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.
For output message, the basic header identifies the application through which SWIFT has processed the message. The basic header also identifies the type of output data, the receiving logical terminal, and (if required) the output session number and output sequence number.
This question is an older one but I wanted to share my code. I'm using a usual table cell view for my section headers. I have designed it with interface builder and implemented the following delegate method.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"Header"];
cell.textLabel.text = @"test";
return cell;
}
You can set explicitly the delegate:
detailViewController.tableView.delegate = detailViewController;
Or you can do it in the controller initial function.
EDIT: your init
method should conform to the canonical init. Furthermore, it seems to me that you have not created your UITableView. Try and use this code:
- (id)initWithStyle:(UITableViewStyle)style {
if ((self = [super initWithStyle:style])) {
self.tableView = [[[UITableView alloc] initWithFrame:self.view.bounds] autorelease];
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth UIViewAutoresizingFlexibleHeight;
self.tableView.delegate = self;
}
return self;
}
Of course, you could also do all of this in a nib file...
Here is how you get the barebones section view up using the UITableViewDelegate methods:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 40.0)];
header.backgroundColor = [UIColor grayColor];
UILabel *textLabel = [[UILabel alloc] initWithFrame:header.frame];
textLabel.text = @"Your Section Title";
textLabel.backgroundColor = [UIColor grayColor];
textLabel.textColor = [UIColor whiteColor];
[header addSubview:textLabel];
return header;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 40.0;
}
You could try this: In your ProjectDetails.h declare a UIView *tableHeader
and also an accessor method - (UIView *)tableHeader;
. Then in the implementation file:
- (UIView *)tableHeader {
if (tableHeader)
return tableHeader;
tableHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];
// addlabel
return tableHeader;
}
In viewDidLoad, call: self.tableView.tableHeaderView = [self tableHeader];
I don't believe you'll need to use the heightForHeaderInSection method.
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