为UITableView节标题返回CGFloat.leastNormalMagnitude导致崩溃

问题描述:

我为iOS 8制作了一个应用程序,该应用程序在其页面上使用了分组UITableView。其中有多个部分使用CGFloat.leastNormalMagnitude(或Swift 2及更低版本中的CGFloat.min)作为部分页眉和页脚高度以删除“默认”空间。一切都进行得很顺利,直到应用程序运行在iOS版9和10,在那里与此错误崩溃:为UITableView节标题返回CGFloat.leastNormalMagnitude导致崩溃

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'section header height must not be negative - provided height for section 0 is -0.00000'

不知何故,1下的任何值(除了四舍五入0)被视为负 - 并使用1作为回报值将使页眉/页脚空间再次出现。

有没有解决这个问题的方法?

在此先感谢。

+0

那么CGFloat.leastNormalMagnitude被视为负数,还是1.0以下的任何值都视为负数? – user3581248

+0

是的,我不知道为什么:| – edopelawi

+0

你试过返回'0'吗?这适用于我 – redent84

我曾尝试几个值的tableView(_:heightForHeaderInSection:),并发现:

  • leastNormalMagnitudeleastNonzeroMagnitude会减去被处理(因此崩溃)。
  • 零将使TableView返回默认高度作为页眉/页脚。
  • 0到1之间的任何值都将被视为负值。
  • 一个会使TableView返回默认高度。
  • 任何超过一个(例如1.1)都会将页眉/页脚设置为实际高度。

我最终使用1.1来解决我的问题。

希望这会帮助那里的人!

+0

我不明白你如何通过设置1.1来解决你的问题 如果你设置高度为1.1,你仍然得到非零空间为您的部分标题吧? – Salah

+0

@Salah是的,我最终填补了1.1空间与一个视图使用相同的背景颜色与表视图。 – edopelawi

我,如果我设定节头高度相同的问题:

tableView.sectionHeaderHeight = UITableViewAutomaticDimension 
tableView.estimatedSectionHeaderHeight = CGFloat.leastNormalMagnitude 

但是,如果我把我的控制器为代表(的UITableViewDelegate)为我的表视图和执行:

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
    return CGFloat.leastNormalMagnitude 
} 

则作品

+0

感谢您的回复,但我的问题发生在'UITableviewDelegate'集合中,没有设置tableView的'sectionHeaderHeight'和'estimatedSectionHeaderHeight '手动:( – edopelawi

也许CGFloat.leastNonzeroMagnitude是你需要的!

+0

谢谢你的答案,但它仍然与此值坠毁:( – edopelawi

有了这样的配置:

tableView.estimatedSectionHeaderHeight = 50.0; 
tableView.sectionHeaderHeight = UITableViewAutomaticDimension; 

我设法把estimatedSectionHeaderHeight正常工作为我的自定义标题,也有部分与根本没有头(无空格),多次尝试和感谢后, edopelawi的解释。

我不得不使用此方法(bannerObject =自定义首部视图,没有bannerObject =甚至没有空格):

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { 
    id bannerObject = [self bannerObjectForSection:section]; 

    if(bannerObject) { 
     return UITableViewAutomaticDimension; 
    } else { 
     return 0.01f; 
    } 
} 

代替这一个,其中甚至没有与UITableViewAutomaticDimension正常工作的自定义标题:

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section { 
    id bannerObject = [self bannerObjectForSection:section]; 

    if(bannerObject) { 
     return 50.0; // UITableViewAutomaticDimension don't calculate the correct size of custom headers 
    } else { 
     return 1.1; 
    } 
}