Harshit Goel
Harshit Goel

Reputation: 679

Set position of the header at the beginning of the TableView

I have to set my headerView at the beginning of the table View. It seems to be fixed please help.

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0,0,tableView.frame.size.width,40)] autorelease];
    headerView.backgroundColor = [Utility consumerLightColor];
    UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 250, headerView.frame.size.height)];
    headerLabel.text = @"Upcoming Appointments";
    headerLabel.textAlignment = NSTextAlignmentLeft;
    headerLabel.textColor = [UIColor whiteColor];
    if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) {
        headerLabel.font = [UIFont fontWithName:@"bauhaus md bt" size:15.0f];
    }
    else{
        headerLabel.font = [UIFont fontWithName:@"bauhaus md bt" size:16.0f];
    }

    // // NSLog(@"title --> %@",[locationArray objectAtIndex:i]);

    headerLabel.backgroundColor = [UIColor clearColor];
    [headerView addSubview:headerLabel];
    [headerLabel release];



    return headerView;

}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (tableView == appointmentListtableView) {
        CGFloat changeheight = 40.00;
        return changeheight;
    }
    else{
        return 0.0f;
    }

}

enter image description here

enter image description here

All I want is to set the position of the header view (Upcoming Appointments)at the beginning of the table View and it should not be fixed.

Upvotes: 1

Views: 1154

Answers (2)

user7609184
user7609184

Reputation:

See keep always in mind,

If you want to set Static header to table view then its good & best practice to set it as a ,

yourTableView.tableHeader = yourHeaderView;

If your headers are dynamic you don't know weather how much sections want to set, then go with method,

viewForHeaderInSection:tableView

That doesn't mean you can use only one at a time not at all, You can use both at a time also,

Now comes to your case, i think you are doing something wrong in the tableViewHader, set it to nil if you don't need anymore,

But i am curious about one thing even if you have set tableViewHeader still why does it show upper & lower side of your secion header,You are setting tableViewHeader or sectionFooter or showing the UITableViewCell

Upvotes: 0

Nex
Nex

Reputation: 71

Maybe you need to remove table header , not section header ? To do this you need to set:

tableView.tableHeaderView = nil

Upvotes: 1

Related Questions