UITableView中节标题的默认高度
-
05-07-2019 - |
题
我想在UITableView中设置第一个标题的高度。对于其他标题,我希望它们保持默认高度。可以用什么值/常数代替“someDefaultHeight”在下面的代码中?
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section == 0)
return kFirstHeaderHeight;
return someDefaultHeight;
}
由于
解决方案
在IOS 5.0及更高版本中,您可以在大多数委托方法中返回UITableViewAutomaticDimension。它位于文档页面的底部
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if(section == CUSTOM_SECTION)
{
return CUSTOM_VALUE;
}
return UITableViewAutomaticDimension;
}
其他提示
从检查我的应用程序中的默认值看,对于分组表,默认值为22,对于非分组表,默认值为10。
如果你检查了你的tableview上属性sectionHeaderHeight的值,应该告诉你。
实际上做的诀窍:))
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if(section == 0)
return kFirstSectionHeaderHeight;
return [self sectionHeaderHeight];
}
为了完整起见:在iOS7 +中,分组样式节标题的高度为第一个 55.5
,后续标题为 38
。
(用DCIntrospect测量)
我不确定这里的正确答案是什么,但iOS 5中的分组表视图的10或22似乎都不正确。我使用的是44,基于这个问题,它至少看起来大致正确的高度。
要获得默认高度,只需让 super
处理它:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section == 0)
return kFirstHeaderHeight;
return [super tableView:tableView heightForHeaderInSection:section];
}
对于swift 4.2,您应该返回UITableView.automaticDimension
这应该可以解决问题
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section == CUSTOM_SECTION)
{
return CUSTOM_VALUE;
}
return [tableView rowHeight];
}
不隶属于 StackOverflow