limon limon - 4 months ago 102
Swift Question

Customize UITableView header section

I want to to customize

UITableView
header for each section. So far, i've implemented

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


this
UITabelViewDelegate
method. What i do want to do is 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

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;
}
Comments