Ios 基于标题文本高度的UITableView节的页眉/页脚高度
我想知道是否可以使UITableView的某个部分的页眉/页脚高度等于页眉/页脚标题文本的高度。任何提示都很好 注意:我的TableView的某些部分可能没有页眉/页脚,在这种情况下,由于“headerTitleHeight/footerTitleHeight”在这种情况下为零,因此在这些部分之间只会有填充Ios 基于标题文本高度的UITableView节的页眉/页脚高度,ios,objective-c,xcode,uitableview,Ios,Objective C,Xcode,Uitableview,我想知道是否可以使UITableView的某个部分的页眉/页脚高度等于页眉/页脚标题文本的高度。任何提示都很好 注意:我的TableView的某些部分可能没有页眉/页脚,在这种情况下,由于“headerTitleHeight/footerTitleHeight”在这种情况下为零,因此在这些部分之间只会有填充 -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return headerTitleHeight + padding;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return footerTitleHeight + padding;
}
可以使用方法获取为页眉/页脚绘制特定NSString所需的高度,然后在UITableView委托方法中返回值。对于没有页眉和页脚的部分,您必须编写代码以仅返回填充。计算文本大小并添加填充高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
CGFloat headerTitleHeight = [jobTitleString sizeWithFont:[UIFont fontWithName:@"OpenSans-Light" size:20.0f] constrainedToSize:CGSizeMake(width,9999)].height;
return headerTitleHeight + padding;
}
你可以试试下面的
CGSize constraint = CGSizeMake(<header/footer width>, <max header/footer height>);
CGSize size;
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
{
NSRange range = NSMakeRange(0, [<string> length]);
NSDictionary *attributes = [<string> attributesAtIndex:0 effectiveRange:&range];
CGSize boundingBox = [<string> boundingRectWithSize:constraint options: NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
size = CGSizeMake(ceil(boundingBox.width), ceil(boundingBox.height));
}
else
{
size = [<string> sizeWithFont:descriptionLabel.font constrainedToSize:constraint lineBreakMode:descriptionLabel.lineBreakMode];
}
return size.height + padding;
CGSize约束=CGSizeMake(,);
cg大小;
如果([[[UIDevice currentDevice]systemVersion]floatValue]>=7.0)
{
NSRange range=NSMakeRange(0,[length]);
NSDictionary*attributes=[attributesAtIndex:0 effectiveRange:&range];
CGSize boundingBox=[boundingRectWithSize:约束选项:NSStringDrawingUserLineFragmentOrigin属性:属性上下文:nil].size;
size=CGSizeMake(ceil(boundingBox.width)、ceil(boundingBox.height));
}
其他的
{
大小=[sizeWithFont:descriptionLabel.font constrainedToSize:constraint lineBreakMode:descriptionLabel.lineBreakMode];
}
返回大小。高度+填充;
或
CGSize maximumLabelSize=CGSizeMake(,);
CGSize expectedSize=[sizeThatFits:maximumLabelSize];
返回expectedSize.height+填充;
您可以
return UITableViewAutomaticDimension;
编辑:Oops,忽略以下内容,请参见下面的注释
或
在
viewDidLoad
,heightforfooterInstitution
和heightForHeaderInSection
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.estimatedSectionFooterHeight = 50; // for footer
self.tableView.estimatedSectionHeaderHeight = 50; // for header
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return UITableViewAutomaticDimension;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return UITableViewAutomaticDimension;
}
在页脚/页眉xib文件中-为您的
UILabel
-添加UILabel约束高度,然后将UILabel约束高度从
Equal
->更改为大于或等于
-最后将UILabel行更改为0
您可以选择自动尺寸标注,但必须具有估计尺寸。它对页脚和页眉的作用方式相同
func tableView(_ tableView: UITableView, estimatedHeightForFooterInSection section: Int) -> CGFloat {
return 300
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return UITableViewAutomaticDimension
}
您使用什么设置页眉/页脚文本?只需使用NSString
方法sizeWithAttributes
(iOS 7)或sizeWithFont…
for(iOS<7)。文档链接这里真棒的答案,这个方法非常有效。谢谢!除了UITableViewAutomaticDimension
是0
,并且0+填充将删除页脚的自动高度功能。@Simon,实际上是-1.0,但您的观点是有效的。编辑原文。正确答案是
return UITableViewAutomaticDimension + padding;
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.estimatedSectionFooterHeight = 50; // for footer
self.tableView.estimatedSectionHeaderHeight = 50; // for header
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return UITableViewAutomaticDimension;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return UITableViewAutomaticDimension;
}
func tableView(_ tableView: UITableView, estimatedHeightForFooterInSection section: Int) -> CGFloat {
return 300
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return UITableViewAutomaticDimension
}