Search code examples
iphoneobjective-chttp-headersgrouped-table

To display Headerview with image and labels in the viewcontroller


I am new to iPhone development. I have created one view controller and I used grouped table view. Now I want to display the header view in my viewcontroller with image and labels. Please guide me and help me out in this problem.

Thanks.


Solution

  • Do you mean a headerView or a sectionHeaderView? You can add subviews to the headerView in the viewDidLoad method:

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 225)];
        label.text = @"BlaBla";
        [self.tableHeaderView addSubview:label];
    }
    

    You specify size and position of the label with the initWithFrame method, and add the label as subview to the tableHeaderView - you can do this with multiple labels.

    If you mean the sectionHeader you have to implement the tableView:viewForHeaderInSection: method, where you have to create a new view, and add different subviews to it:

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
    
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 40)];
        label.text = @"BlaBla";
        [view addSubview:label];
        [label release];
    
        return [view autorelease];
    }
    

    In this case you also have to implement the method tableView:heightForHeaderInSection: which has to return the height of the view you create in the above method:

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
        return 50.0f;
    }