Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView titleForHeaderInSection is not being called

I add a tableview to a view controller then try to change its section header titles in IOS 6.0.

I will be changing header strings every-time a specific function is called so I dont want to deal with (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

I tried to use (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section but when I put a breakpoint I see that its not being called.

in.h file I add UITableViewDelegate,UITableViewDataSource

in.m

-(void)viewDidAppear:(BOOL)animated
{
   //tableview init
    self.meetingList=[[UITableView alloc] initWithFrame:CGRectMake(10, self.ckCal.frame.origin.y + self.ckCal.bounds.size.height+20, 384, 450) style:UITableViewStylePlain];
    [self.meetingList setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    self.meetingList.delegate=self;

    //populate mutablearray for tableview here
    [self eventsInThisMonth:[NSDate date]];

    [self.view addSubview:self.meetingList];
}
-(void)eventsInThisMonth:(NSDate *)date
{

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter  setDateFormat:@"MMMM , yyyy"];
    //self.firstSectionHeader=nil;
    self.firstSectionHeader= [NSString stringWithFormat:@"Events in %@", [dateFormatter stringFromDate:date]];
    NSLog(@" self.firstSectionHeader %@ ",self.firstSectionHeader);

}
#pragma Tableview
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.eventHeaders count] + 1;

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 35;
}
//set header section labels
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 35)] ;
    [headerView setBackgroundColor:[UIColor colorWithRed:106/256.0 green:106/256.0 blue:106/256.0 alpha:1.0]];
    UILabel *subjectLabel = [[UILabel alloc] initWithFrame:CGRectMake(92, 10, tableView.bounds.size.width, 20)];
    subjectLabel.textColor = [UIColor whiteColor];
    subjectLabel.font = [UIFont fontWithName:@"Gill Sans" size:20];
    subjectLabel.backgroundColor = [UIColor clearColor];
    subjectLabel.text=self.firstSectionHeader;
    NSLog(@"subjectLabel.text %@",subjectLabel.text);

    if (section==0) {

        //[headerView addSubview:subjectLabel];
        return headerView;
    }
    else
        return nil;


}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section==0) {
         NSLog(@"self.firstSectionHeader in titleForHeaderInSection %@",self.firstSectionHeader);
        return self.firstSectionHeader;
    }
    else
        return nil;
}

What am I doing wrong?

like image 479
user2858870 Avatar asked Oct 08 '13 13:10

user2858870


3 Answers

I think that's probably because you are only supposed to use one of the two: viewForHeaderInSection or titleForHeaderInSection.

like image 79
Odrakir Avatar answered Nov 05 '22 14:11

Odrakir


You are missing self.meetingList.datasource = self;

Remember that your viewController has to implemente the UITableViewDelegate and the UITableViewDataSource.

YourClass : UITableView <UITableViewDelegate, UITableViewDataSource>
like image 1
artud2000 Avatar answered Nov 05 '22 16:11

artud2000


Re: What am I doing wrong.

I am not certain if you are setting datasource and delegate on your view controller in Interface Builder or not. But I believe there is a logic error that impacts the expected outcome.

titleForHeaderInSection: Asks the data source for the title of the header of the specified section of the table view.

meanwhile

tableView:viewForHeaderInSection: Asks the delegate for a view object to display in the header of the specified section of the table view.

The latter method is overriding behavior defined for the titleForHeaderInSection. Think of it this way; you have one header space that can hold a title OR a custom view that you create to replace the default (title). You are telling the compiler to use a custom view by implementing the second method. To fix this comment out or remove the tableView:viewForHeaderInSection: method or change your logic so that you update the title in your custom view that you pass into the method. At that point titleForHeaderInSection should be hit correctly.

Please consider accepting this answer if it is successful in resolving your initial question.

like image 1
Tommie C. Avatar answered Nov 05 '22 15:11

Tommie C.