UITableView:自定义标题标题视图不显示

时间:2012-08-23 09:04:08

标签: iphone ios uitableview ios5

我想显示一个包含自定义标题标题的表格。 table view附加到实现controller class和数据源协议但不是tableview delegate的子类的UIViewController,因为该表是要在另一个tableview上方显示的子视图

我的代码的一些片段: tableview以编程方式创建:

    _myListView = [[UITableView alloc] initWithFrame:tableFrame style:UITableViewStyleGrouped];

[_myListView setDataSource:self.myListController];
[_myListView setDelegate:self.myListController];
[_myListView setBackgroundColor:darkBackgroundColor];

其中myListController是类中的强属性。

对于部分中的行数:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    …
    return count;    
}

部分数量:

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [someDelegate sectionCount];
}

对于自定义标题视图:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView* headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, SectionHeaderHeight)];
    UILabel* sectionHeaderTitle = [[UILabel alloc] initWithFrame:CGRectMake(20, 3, 300, 24)];

    [headerView setBackgroundColor:[UIColor clearColor]];

    sectionHeaderTitle.text = [self myTitleForHeaderInSection:section];
    sectionHeaderTitle.textColor = [UIColor whiteColor];
    sectionHeaderTitle.textAlignment = UITextAlignmentLeft;

   [headerView addSubview:sectionHeaderTitle];

    return headerView;
}

对于自定义headerViewHeight(从iOS5开始需要):

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if ( [self tableView:tableView numberOfRowsInSection:section] > 0) {
        return SectionHeaderHeight;
    } else {
        return 0;
    }
}

可悲的是,tableview没有显示任何节标题,就像我会返回nil一样。 但是,我已经检查过断点,代码实际上返回了一个UIView。

其他一切正常。

我错过了什么?请不要犹豫,让我为自己感到羞耻。

4 个答案:

答案 0 :(得分:1)

我真的不明白为什么要使用自定义视图,而不是“标准”视图?你可能有理由,但我的代码中没有看到任何告诉我原因的原因:)

我个人只是用这个:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 0) return @"First section header title";
    if (section == 1) return @"Second section header title";
    else return nil;
}

告诉我,这不是你想要的!

答案 1 :(得分:0)

文字颜色您将其更改为黑色并检查一次。

sectionHeaderTitle.textColor = [UIColor blackColor];

答案 2 :(得分:0)

我似乎找到了解决方案:

我为每个要显示的标题视图创建了一个延迟加载强属性。 (幸运的是只有三个)

现在显示视图。

在呈现表之前,似乎没有强引用就已取消分配标题视图。

是否存在与实现表视图委托的类的连接,而数据源协议不是UIViewController?

答案 3 :(得分:0)

你尝试这个代码这项工作在我身边: - )

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 24)];

    UIImage *myImage = [UIImage imageNamed:@"SectionBackGround.png"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:myImage];
    imageView.frame = CGRectMake(0,0,320,24);

    UIImage *imageIcon = [UIImage imageNamed:@"SectionBackGround.png"];
    UIImageView *iconView = [[UIImageView alloc] initWithImage:myImage];
    iconView.frame = CGRectMake(0,0,320,24);

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 24)];
    label.text = [tableView.dataSource tableView:tableView titleForHeaderInSection:section];
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont boldSystemFontOfSize:14];


    [view addSubview:imageView];
    [view addSubview:iconView];
    [view addSubview:label];
    return view;

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 24;
}