iCode
iCode

Reputation: 1466

Add a view under the set header in a section

I want to add a view under the header of a section in a UITableView. Is that possible? Here's my code so far, with that code it replaces the section:

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 4) {
        return 10;
    } else
    {
        return 20;
    }
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 4) {
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 10)];
        view.backgroundColor = [UIColor blackColor];
        return view;
    } else {
        return nil;
    }
}

Upvotes: 1

Views: 251

Answers (1)

Mick MacCallum
Mick MacCallum

Reputation: 130193

Conceptually, I wouldn't think of this as adding another view underneath your header. Instead, consider making your header the height that you want the header to be + the height of the black line. Then, all you have to do is create an additional view (the black line) and add it as a sub view of the header, ex

UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 10)];
view.backgroundColor = [UIColor whiteColor];

UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0.0, view.frame.size.height - 2.0, view.frame.size.width, 2.0)];
[lineView setBackgroundColor:[UIColor blackColor]];

[view addSubview:lineView];

Upvotes: 1

Related Questions