我想显示带自定义标题标题的表。 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。
其他一切正常。
我错过了什么?请放心,不要犹豫,让我为自己感到羞愧。
你为什么不能设置背景颜色为你的“headerView”和检查,它是可见的?编辑:*也为'sectionHeaderTitle'标签... – Nina
已经做到了,尝试redColor都没有成功 –
我试过你的'viewForHeaderInSection'在我的项目。它对我来说工作得很好。 – Nina