Table header question

Discussion in 'iOS Programming' started by beachdog, Dec 6, 2008.

  1. beachdog macrumors member

    Aug 10, 2008
    I have a table and simply want the header to be drawn in the same font that is used when I look at it in Interface Builder, when using the grouped table style (e.g. "California" in the attached screen shot).

    My understanding is that even to just have a simple text title for a heading I need to implement

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    and create a UILabel -- is that correct? Seems like a lot of work when you just want to supply simple text for the header, but oh well.

    So I have done that, but my question is what specific font is used in Interface Builder that I want to use, and also what text color is that? My current code looks like this, but does not look quite the same:

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UILabel* hdr = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 280, 40)] autorelease];
    hdr.textAlignment = UITextAlignmentCenter;
    hdr.font = [UIFont systemFontOfSize:16.0];
    hdr.opaque = YES ;
    hdr.textColor = [UIColor darkGrayColor];
    hdr.text = NSLocalizedString(@"my-header-title",@"");
    hdr.backgroundColor = [UIColor clearColor];
    return hdr ;

    Attached Files:

  2. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    If you implement

    - (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section
    then you just have to return the string and the table will render it the way you see it in IB. You can also return nil for no header. You should realize that there are headers for sections and a single header for the table that can be set independently.
  3. l.sanjeevrao macrumors newbie

    Oct 17, 2008
    Header,Footer height for UITableView

    Hi, Here are two ways to set the uitableview’s footer,header height settings.

    Set with Properities.
    set with delegate methods.

    Setting with properties .
  4. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
  5. gujamin macrumors member

    Aug 6, 2002
    I too would like the answer to this question about creating the font programmatically for use in your own cells
  6. woodmantech macrumors newbie

    Mar 7, 2011
    Example viewForHeaderInSection

    I don't have the exact colors for the text and shadow that you are looking for, but the basic code to do what you are looking for is as follows:

    -(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {   // custom view for header. will be adjusted to default or specified header height
    	UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 24.0f)];
    	headerView.backgroundColor = [UIColor clearColor];
    	UILabel *headerTitle = [[[UILabel alloc] initWithFrame:CGRectMake(10.0f, 0.0f, 300.0f, 24.0f)] autorelease];
    	[headerTitle setBackgroundColor:[UIColor clearColor]];
    	[headerTitle setFont:[UIFont boldSystemFontOfSize:14.0f]];
    	[headerTitle setTextColor:[UIColor darkGrayColor]];
    	[headerTitle setShadowColor:[UIColor shadowColor]];
    	[headerTitle setShadowOffset:CGSizeMake(0.0f, 1.0f)];
    	switch (section) {
    		case 0:
    			[headerTitle setText:@"Heading 1"];
    		case 1:
    			[headerTitle setText:@"Heading 2"];
    			[headerTitle setText:@""];
    	[headerView addSubview:headerTitle];
    	return headerView;

Share This Page