我将一个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;
}
我做错了什么?
答案 0 :(得分:15)
我认为这可能是因为你只应该使用其中一个:viewForHeaderInSection
或titleForHeaderInSection
。
答案 1 :(得分:1)
您缺少self.meetingList.datasource = self;
请记住,你的viewController必须实现UITableViewDelegate和UITableViewDataSource。
YourClass : UITableView <UITableViewDelegate, UITableViewDataSource>
答案 2 :(得分:1)
回复:我做错了什么。
我不确定您是否在Interface Builder中的视图控制器上设置数据源和委托。但我认为存在影响预期结果的逻辑错误。
titleForHeaderInSection: 向数据源询问表视图的指定部分的标题标题。
同时
的tableView:viewForHeaderInSection: 要求委托使视图对象显示在表视图的指定部分的标题中。
后一种方法是覆盖为titleForHeaderInSection定义的行为。这样想吧;您有一个标题空间可以容纳标题或您创建的自定义视图替换默认(标题)。您告诉编译器通过实现第二种方法来使用自定义视图。要修复此注释,请删除tableView:viewForHeaderInSection:方法或更改逻辑,以便更新传递给方法的自定义视图中的标题。此时,应正确命中titleForHeaderInSection。
如果成功解决您的初步问题,请考虑接受此答案。
答案 3 :(得分:0)
你必须做
self.meetingList.dataSource=self;
在viewDidAppear中。
答案 4 :(得分:0)
我知道这个帖子有点旧,但请确保你也有:
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
在查看视图之前,tableview需要知道高度。
答案 5 :(得分:0)
此外,您不需要实现两者
Name:Orange<br/>
Count:5<br/>
Name:Mango<br/>
Count:6<br/>
Name:Banana<br/>
Count:3<br/>
和
(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
如果要实现至少第一种方法,那么
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
不会被调用。