ios 未调用UITableView titleForHeaderInSection

n53p2ov0  于 2023-06-07  发布在  iOS
关注(0)|答案(7)|浏览(155)

我在一个视图控制器中添加了一个tableview,然后尝试在IOS6.0中更改它的节头标题。
每次调用特定函数时,我都会更改头字符串,因此我不想处理(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
我尝试使用(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section,但是当我设置断点时,我发现它没有被调用。
在. 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;
}

我做错了什么?

efzxgjgh

efzxgjgh1#

我想这可能是因为你只应该使用两个中的一个:viewForHeaderInSectiontitleForHeaderInSection

x6492ojm

x6492ojm2#

您缺少self.meetingList.datasource = self;
记住你的viewController必须实现UITableViewDelegate和UITableViewDataSource。

YourClass : UITableView <UITableViewDelegate, UITableViewDataSource>
ldxq2e6h

ldxq2e6h3#

Re:我做错了什么
我不确定你是否在界面生成器中的视图控制器上设置了数据源和委托。但我相信有一个逻辑错误影响了预期的结果。
titleForHeaderInSection:向数据源询问表视图的指定节的标头标题。
同时
tableView:viewForHeaderInSection:向委托请求要在表视图的指定节的标头中显示的视图对象。
后一个方法覆盖为titleForHeaderInSection定义的行为。这样想吧你有一个标题空间,可以保存一个标题或一个自定义视图,你创建来取代默认的(标题)。通过实现第二个方法,您正在告诉编译器使用自定义视图。要修复此注解或删除tableView:viewForHeaderInSection:方法或更改逻辑,以便更新传递到该方法中的自定义视图中的标题。此时应该正确命中titleForHeaderInSection。
如果这个答案成功地解决了你最初的问题,请考虑接受它。

niknxzdl

niknxzdl4#

我知道这篇文章有点旧,但请确保您也有:
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
tableview在获取视图之前需要知道高度。

fslejnso

fslejnso5#

另外,您不需要同时实现这两个

(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

和/或

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

如果您将实现至少一个first方法,则

- (NSString*)tableView:(UITableView *)tableView  titleForHeaderInSection:(NSInteger)section

不会被传唤

yizd12fk

yizd12fk6#

heightForHeaderInSectiontitleForHeaderInSection似乎都是必需的。只实现titleForHeaderInSection对我不起作用,添加heightForHeaderInSection解决了这个问题。

y4ekin9u

y4ekin9u7#

你必须做

self.meetingList.dataSource=self;

in viewDidAppear.

相关问题