ios - UITableView 动态高度不改变 iOS
问题描述
拖放 UITableViewHeader
你可以看一个橙色的 UITextView。
我将tableview的高度常数设置为零。
重新加载 UITableView 的 tableview 总高度后显示与以前相同(因为没有 UITextView 显示)
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.heightConstCommentBox.constant = 0;
[self configureTableviewFooter];
}
-(void)configureTableviewFooter{
//More button Configure
height = 50 +[self recusiveDescription:self.viewFooter] ;
// Assign new frame
self.viewFooter.frame = CGRectMake(self.viewFooter.frame.origin.x,
self.viewFooter.frame.origin.y,self.viewFooter.frame.size.width , height); // viewFooter is container view of tableFooterView
self.tableView.tableFooterView = self.viewFooter;
[self.tableView reloadData];
}
- (float )recusiveDescription:(UIView *)view
{
NSString *s = @"";
float height = 0;
NSArray *subviews = [view subviews];
if ([subviews count] == 0) return 0;
for (UIView *subView in subviews) {
height = height + subView.frame.size.height ;
[self recusiveDescription:subView];
}
return height;
}
重新加载 tableview 后,表格视图页脚大小不会改变。
解决方案
如果您使用自定义单元格作为表格,您可以使用以下内容。
在以下代码中,将posts[index.row].post替换为您的标签内容,+230 是您单元格的最小高度。或者你可以说它在标签以外的其他视图的高度。
仅当您有 1 个标签或 1 个文本字段、1 个文本视图时,自动尺寸方法才有效。
并且不要固定标签的高度。
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let heightOfRow = self.calculateHeight(inString: postss[indexPath.row].post)
return (heightOfRow + 230)
}
//custom function for calculate dynamic height
func calculateHeight(inString:String) -> CGFloat
{
let messageString = inString
let attributes : [NSAttributedString.Key : Any] = [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 15.0)]
let attributedString : NSAttributedString = NSAttributedString(string: messageString, attributes: attributes)
let rect : CGRect = attributedString.boundingRect(with: CGSize(width: 370, height: CGFloat.greatestFiniteMagnitude), options: .usesLineFragmentOrigin, context: nil)
let requredSize:CGRect = rect
return requredSize.height
}
推荐阅读
- java - 我们可以将父项目中定义的用户定义类用于子项目maven吗?
- rabbitmq - 如何使用 MassTransit 和 RabbitMQ 设置消费者优先级
- javascript - 为什么使用 mongoose 删除 mongodb 集合中的数据后集合模式的长度没有改变?
- haskell - Haskell Attoparsec 无限循环
- toolkit - 使用 Holotoolkit 滑块的值
- android - Android ImageView程序,如何根据随机函数设置imageview
- c - 我的最终功能不会返回
- xcode - 没有故事板的编码
- excel - 代码似乎永远运行并且错误:未设置块变量(VBA)
- jersey-2.0 - 带有 Guice 的 Jersey 2.27 - 没有 HK2-guice 桥是否支持