繁体   English   中英

viewForHeaderInSection 和 titleForHeaderInSection 都被称为

[英]viewForHeaderInSection and titleForHeaderInSection are both called

我有以下类: ParentVC:UITableViewControllerSubClassVC:ParentVC都实现titleForHeaderInSection

但是,我想开始在SubClassVC SubClassVC添加了viewForHeaderInSection 我注意到SubClassVC中的viewForHeaderInSectiontitleForHeaderInSection都被调用,即使viewForHeaderInSection应该具有优先权。 如果我从SubClassVC中删除titleForHeaderInSection ,则仍然调用ParentVC导致相同的问题。 两者都被调用导致重复的重叠标题。

有什么想法可以阻止titleForHeaderInSection被调用吗? 应该是这样吗? 这在 Obj-C 中,但在 Swift 中也可能是一个问题。


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
// other code
    return view;
}

- (NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section
{
    return nil;
}

解决此问题的一种方法是使用组合而不是 inheritance。 您可以在 SubClassVC 中自己实现它,而不是依赖父级来实现 UITableViewDataSource 协议,并且在实现时 - 重用 ParentVC 的代码。 重用该代码的一种方法是将数据源方法重构为单独的 class(将这些方法提取到新的 class MyTableDataSource,然后在 ParentVC 和 SubClassVC 中创建并调用它)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM