Customize UITableView header section

limon picture limon · Mar 25, 2013 · Viewed 258k times · Source

I want to customize UITableView header for each section. So far, I've implemented

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

this UITabelViewDelegate method. What I want to do is to get current header for each section and just add UILabel as a subview.

So far, I'm not able to accomplish that. Because, I couldn't find anything to get default section header. First question,is there any way to get default section header ?

If it's not possible, I need to create a container view which is a UIView but,this time I need to set default background color,shadow color etc. Because, if you look carefully into section's header, it's already customized.

How can I get these default values for each section header ?

Thank you all.

Answer

Lochana Ragupathy picture Lochana Ragupathy · Mar 25, 2013

You can try this:

 -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 18)];
    /* Create custom view to display section header... */
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, tableView.frame.size.width, 18)];
    [label setFont:[UIFont boldSystemFontOfSize:12]];
     NSString *string =[list objectAtIndex:section];
    /* Section header is in 0th index... */
    [label setText:string];
    [view addSubview:label];
    [view setBackgroundColor:[UIColor colorWithRed:166/255.0 green:177/255.0 blue:186/255.0 alpha:1.0]]; //your background color...
    return view;
}