2012-08-23 81 views
1

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

我的一些代码片段: 的实现代码如下的编程方式创建:

_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不显示任何节标题,就像我会返回零。 但是,我已经检查了一个断点,该代码实际上返回一个UIView。

其他一切正常。

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

+0

你为什么不能设置背景颜色为你的“headerView”和检查,它是可见的?编辑:*也为'sectionHeaderTitle'标签... – Nina

+0

已经做到了,尝试redColor都没有成功 –

+0

我试过你的'viewForHeaderInSection'在我的项目。它对我来说工作得很好。 – Nina

回答

0

我似乎已经找到了解决方案:

我已经创建了每个标题视图我想显示迟缓装载强属性。 (幸运的是只有三个)

现在显示视图。

似乎在表格呈现之前,标题视图没有强引用而被释放。

难道是有一个实现表视图的类的连接委托和数据源协议不是UIViewController?

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

告诉我,如果这就是你要找没有什么!

+0

好的,我可能错过了第一行,说:“我想显示一个自定义标题标题的表格。”。如果我完全偏离主题,我很抱歉:) – rdurand

+0

我想要显示自定义标题的主要原因是在表视图中黑暗的背景,并且我想在标题中显示一个图标。但我不介意偏离主题的答案;) –

+0

'heightForHeaderInSection'是否返回正确的值?你用断点检查过吗? – rdurand

0

将文本颜色更改为黑色并检查一次。

sectionHeaderTitle.textColor = [UIColor blackColor];

+0

桌面视图背景对于黑色文本颜色已经很暗,但我尝试了蓝色,但仍然没有标题可见。 –

+0

我试过了你提供的任何代码。只是改变了文字颜色,它对我来说工作得很好。 –

+0

尝试将heightForHeaderInSection设置为40或任何有效值。 –

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