Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to recreate the default UIView the same as the default tableView:viewForHeaderInSection:?

I tried to implement the

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

to get the text label of the header in section of black color instead of the white color, but it looks so different from the default one created by the SDK, mine is so ugly.

How to recreate the UIView with exactly the same as the one from the SDK?

From Apple documentation:

Discussion
The table view uses a fixed font style for section header titles. If you want a different font style, return a custom view (for example, a UILabel object) in the delegate method tableView:viewForHeaderInSection: instead.

like image 416
Hoang Pham Avatar asked May 24 '10 16:05

Hoang Pham


2 Answers

Although this is not exactly right, I'm posting this in hopes that someone can improve upon my approach. The text is right (white like default, but you can change that). The background gradient and opacity are not perfect (too light??)--some tweaking here might be needed.

Get the sectionheaderbackground.png here: http://img5.imageshack.us/img5/6616/sectionheaderbackground.png

// Create a stretchable image that emulates the default gradient
UIImage *buttonImageNormal = [UIImage imageNamed:@"sectionheaderbackground.png"];
UIImage *stretchableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];

// Create the view for the header
CGRect sectionFrame = CGRectMake(0.0, 0.0, 320.0, 22.0);
UIView *sectionView = [[UIView alloc] initWithFrame:sectionFrame];
sectionView.alpha = 0.9;
sectionView.backgroundColor = [UIColor colorWithPatternImage:stretchableButtonImageNormal];

// Create the label
CGRect labelFrame = CGRectMake(10.0, 0.0, 310.0, 22.0);
UILabel *sectionLabel = [[UILabel alloc] initWithFrame:labelFrame];
sectionLabel.text = @"Test section label";
sectionLabel.font = [UIFont boldSystemFontOfSize:18.0];
sectionLabel.textColor = [UIColor whiteColor];
sectionLabel.shadowColor = [UIColor grayColor];
sectionLabel.shadowOffset = CGSizeMake(0, 1);
sectionLabel.backgroundColor = [UIColor clearColor];
[sectionView addSubview:sectionLabel];
[sectionLabel release];

// Return the header section view
return sectionView;
like image 174
dferg Avatar answered Nov 18 '22 01:11

dferg


You can get the default UITableView section headers by implementing the following methods in your tableview class:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{   
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 22; //returning 0 hides section headers
}

This might be particularly useful if you use a UISearchDisplayController in your app, where you want the search results to be displayed in a simple UITableViewStylePlain fashion instead of, let's say, the UITableViewStyleGrouped of your customized main tableview. You could then implement something like this:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{   
    if ([self.searchDisplayController.searchBar isFirstResponder]) return nil;
    //create and return your custom UIView here
}
like image 39
DavidD Avatar answered Nov 18 '22 03:11

DavidD