Friday, January 3, 2014

Add a UITableView Header in iOS Application



- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 20)];
    UIImageView *headerImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"heade2.png"]];
    //set your image/
    
    UILabel *headerLbl = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 205, 15)];
    //set as you need
    headerLbl.backgroundColor = [UIColor clearColor];
   // headerLbl.text = [self.listOfHeader objectAtIndex:section];
    headerLbl.text =@"Affrica";
    headerLbl.textColor =[UIColor whiteColor];
    
    headerLbl.textAlignment = UITextAlignmentCenter;
    
    [headerImage addSubview:headerLbl];
    
    headerImage.frame = CGRectMake(0, 0, tableView.bounds.size.width, 20);
    
    [headerView addSubview:headerImage];
    
    return headerView;
}



you can easily set height for header using this method.



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