UITableView:自定义标题标题视图不显示 [英] UITableView: custom header title view doesn't show
问题描述
我想显示一个包含自定义标题标题的表格。
表视图
附加到实现 tableview委托的
和数据源协议但不是控制器类
UIViewController
的子类,因为该表是要在另一个tableview上方显示的子视图。
I want to display a table with custom header titles.
The table view
is attached to a controller class
that implements the tableview delegate
and data source protocols but is not a subclass of UIViewController
because the table is a subview to be displayed above another tableview.
我的代码的一些片段:
tableview是以编程方式创建的:
some snippets of my code: The tableview is created programmatically:
_myListView = [[UITableView alloc] initWithFrame:tableFrame style:UITableViewStyleGrouped];
[_myListView setDataSource:self.myListController];
[_myListView setDelegate:self.myListController];
[_myListView setBackgroundColor:darkBackgroundColor];
其中myListController是类中的强属性。
where myListController is a strong property in the class.
对于部分中的行数:
-(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开始需要):
For the custom headerViewHeight (as required since iOS5):
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if ( [self tableView:tableView numberOfRowsInSection:section] > 0) {
return SectionHeaderHeight;
} else {
return 0;
}
}
遗憾的是,tableview不显示任何节标题好像我会回到零。
但是,我已经检查了一个断点,代码实际上返回了一个UIView。
Sadly, the tableview does not display any section headers just as if I would return nil. However, I have checked with a breakpoint, that the code actually returns an UIView.
其他一切正常。
我错过了什么?请不要犹豫,让我为自己感到羞耻。
What am I missing? PLease, don't hesitate to make me feel ashamed of my self.
推荐答案
我似乎找到了解决方案:
I seem to have found a solution:
我为每个要显示的标题视图创建了一个延迟加载强属性。 (幸运的是只有三个)
I have created a lazy loading strong property for each header view I want to display. (luckily there are only three)
现在显示了视图。
看来标题视图得到了在表格呈现之前,在没有强引用的情况下取消分配。
It seems that the header views got deallocated without the strong references before the table was rendered.
是否存在与实现表视图委托的类的连接,而数据源协议不是UIViewController?
Could it be that there is a connection to the class implementing the table view delegate and data source protocols is not a UIViewController?
这篇关于UITableView:自定义标题标题视图不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!