未调用UITableView titleForHeaderInSection

use*_*870 5 objective-c uitableview ios

我将一个tableview添加到视图控制器,然后尝试在IOS 6.0中更改其节标题标题.

我将在每次调用特定函数时更改标题字符串,因此我不想处理 (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

我尝试使用 (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section但是当我设置断点时,我发现它没有被调用.

在in.h文件中我添加了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;
}
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

Odr*_*kir 16

我想这可能是因为你只应该使用两者之一:viewForHeaderInSection或者titleForHeaderInSection.

  • 完全错误.它们都会以已知的顺序被调用,事实上这是一个非常合理的事情 - 例如在`titleFor`中给它一个标题字符串,然后在`viewFor`中调整它的外观. (2认同)