ios - 未调用 UITableView titleForHeaderInSection

标签 ios objective-c uitableview

我将一个 TableView 添加到 View Controller ,然后尝试在 IOS 6.0 中更改其节标题。

每次调用特定函数时我都会更改 header 字符串,所以我不想处理 (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

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

在.h文件中我添加了UITableViewDelegate,UITableViewDataSource

英寸.米

-(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;
}

我做错了什么?

最佳答案

我认为这可能是因为您应该只使用两者之一:viewForHeaderInSectiontitleForHeaderInSection

关于ios - 未调用 UITableView titleForHeaderInSection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19249783/

相关文章:

ios - 如何在播放同一个本地视频时同步多个 AVPlayer

objective-c - 更改 nib 文件加载的内容

ios - xcode 通过 ruby​​ gem xcodeproj 更改代码签名身份

ios - iCloud 中的 CoreData 和照片问题

IOS - 最新版本的默认 TableView 单元格分隔符颜色

ios - objective-c - 标签中的多种颜色

objective-c - 在主线程或后台线程上进行iOS XML解析

ios - NSAttributedString - 获取字体属性

ios - 为什么我不能从嵌入式 UITableViewController 调用存储在父 UIViewController 中的方法?

ios - 将 UIViewController 嵌入到导航 Controller 中,但删除了手动 UINavigationBar 的导航 Controller ,但现在出错了