PDA

View Full Version : Table header question




beachdog
Dec 6, 2008, 08:53 AM
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 ;
}



PhoneyDeveloper
Dec 6, 2008, 01:07 PM
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.

l.sanjeevrao
Dec 17, 2008, 02:25 PM
Hi, Here are two ways to set the uitableview’s footer,header height settings.

Set with Properities.
set with delegate methods.

Setting with properties . http://iappdevs.com/?p=139

PhoneyDeveloper
Dec 17, 2008, 03:35 PM
How does that affect the font of the header?

gujamin
Feb 10, 2009, 04:23 PM
I too would like the answer to this question about creating the font programmatically for use in your own cells

woodmantech
Mar 7, 2011, 12:24 PM
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"];
break;
case 1:
[headerTitle setText:@"Heading 2"];
break;
default:
[headerTitle setText:@""];
break;
}

[headerView addSubview:headerTitle];
return headerView;
}