2013-10-08 41 views
5

我向视图控制器添加了一个tableview,然后尝试在IOS 6.0中更改其节标题标题。UITableView titleForHeaderInSection未被调用

我会改变头字符串的每次特定的函数被调用,所以我不希望处理(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; 
} 

我在做什么错?

+0

看着你的代码...你为什么要使用viewForHeaderInSection和titleForHeaderInSection?无论如何,您在viewForHeaderInSection中设置标题... – Odrakir

回答

15

我认为这可能是因为你只能使用其中的一个:viewForHeaderInSectiontitleForHeaderInSection

+0

如果我想更改标题部分的背景颜色,该怎么办? – user2858870

+0

您只需在viewForHeaderInSection中创建一个新的标题。 – Odrakir

+1

你可以实现两者。在viewForHeaderInSection我假设你有一个UILabel(它拥有你使用标题的任何NSString)。只需通过调用titleForHeaderInSection来设置该字符串: 这两个世界都是最好的。 – Doc

1

你缺少self.meetingList.datasource = self;

记住,你的viewController有implemente的的UITableViewDelegate和UITableViewDataSource。

YourClass : UITableView <UITableViewDelegate, UITableViewDataSource> 
+0

你的答案是一半正确的谢谢,我试图upvote你的答案,但我不能upvote没有足够的声誉。 – user2858870

+0

我看到太坏:( – artud2000

0

你要做

self.meetingList.dataSource=self; 

在viewDidAppear。

1

回复:我做错了什么。

我不确定你是否在Interface Builder中的视图控制器上设置数据源和委托。但我相信有一个逻辑错误会影响预期的结果。

titleForHeaderInSection: 向数据源询问表视图指定部分标题的标题。

同时

的tableView:viewForHeaderInSection: 询问委托视图对象在表视图的指定部分的标题显示。

后一种方法是覆盖为titleForHeaderInSection定义的行为。这样想一想;你有一个标题空间可以容纳一个标题或你创建的自定义视图来替换默认(标题)。您正在通过实施第二种方法告诉编译器使用自定义视图。要修正此注释或删除tableView:viewForHeaderInSection:方法或更改逻辑,以便更新传入该方法的自定义视图中的标题。在那点titleForHeaderInSection应该正确点击。

如果成功解决了您的初始问题,请考虑接受此答案。

0

我知道这个线程是有点老了,但要确保你也有:

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

的tableview中需要知道的高度,才可以抓住视图。

0

此外,还需要不实现这两个

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

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

如果你要实现一个至少第一种方法,然后

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

不会被调用。