Configure header section for UITableViewController

I need to configure the header section of the UITableViewController , where a different header text is returned for each section (getting data from the data source 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 see the expected result. However, I also need to change the font size of the text and, looking at similar questions, I 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. I realized that the UITableViewController sets the default by itself as a delegate, but it seems like I'm wrong.

UITableViewController is created 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 should I make for this to work? Thanks.

+6
source share
4 answers

You can explicitly specify a delegate:

  detailViewController.tableView.delegate = detailViewController; 

Or you can do this in the initial function of the controller.

EDIT: your init method should match the canonical init. In addition, it seems to me that you have not created your UITableView. Try using 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 do all this in a nib file ...

+3
source

This question is older, but I wanted to share my code. I use the normal table cell view for section headings. I developed it using the interface builder and implemented the following delegation method.

 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"Header"]; cell.textLabel.text = @"test"; return cell; } 
+11
source

Here's how you get the view in the barebones section 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; } 
+3
source

You can try the following: in ProjectDetails.h, declare UIView *tableHeader , and the access 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 do not think you need to use the heightForHeaderInSection method.

+2
source

Source: https://habr.com/ru/post/889452/


All Articles