[英]Resizing UICollectionViewCell
我正在使用UICollectionView显示记录,对于一条记录,我创建了一个xib文件,如下所示
1。 标题网页视图包含帖子标题(如果有)2。 Imageview包含帖子图像(如果有)3 .Text webview包含帖子文本(如果有)
数据来自远程服务器。 我创建了一个单独的UIcollectionViewCell类来设置数据。
我想根据单元格数据调整UIcollectionViewCell的大小。 以前我在压倒一切
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return size
}
完成它,但对我来说并不完美。 如何根据单元格的大小调整单元格的大小
1。 标题html
2。 图片
3。 文字html
现在我想做的是
在我的UIcollectionViewCell类中
if text != nil {
postTextWebView.delegate = self
postTextWebView.loadHTMLString(text!, baseURL: bundleURL as URL)
}
else{
postTextWebView.loadHTMLString("", baseURL: nil)
postTextHeightConstraint.constant = 0
}
此类重写UIWebViewDelegate的webViewDidFinishLoad()函数
func webViewDidFinishLoad(_ webView: UIWebView) {
var frame : CGRect = webView.frame;
frame.size.height = 1;
webView.frame = frame;
let fittingSize : CGSize = webView.sizeThatFits(.zero)
frame.size = fittingSize;
webView.frame = frame;
if let sizeCalculator = self.dynamicSizeCalculator {
sizeCalculator.calculateVariableSize(tag: postTextWebView.tag, variableSize: fittingSize.height, indexPath: IndexPath.init(row: postTextWebView.tag, section: 0))
}
}
protocol DynamicSizeCalculator:class{
func calculateVariableSize(tag:Int,variableSize:CGFloat,indexPath:IndexPath)
}
在我的View控制器类中
var variableHeights:[CGFloat]? = []
var isFirstTimeLoaded : Bool = false
从服务器获取数据时,布尔值变为true
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
self.collectionView.collectionViewLayout.invalidateLayout()
super.viewWillTransition(to: size, with: coordinator)
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell:Cell
cell = collectionView.dequeueReusableCell(withReuseIdentifier: "softBoardPostCell", for: indexPath) as! Cell
cell.postTextWebView.tag = indexPath.row
cell.dynamicSizeCalculator = self
cell.drawCell(posts[indexPath.row])
cell.sbActionChosenListner=self
return cell
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return posts.count
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
// var cellHeight:CGFloat = 20 + 19.5 + 9.5+14.5+8+16+9+16+10+34+12+14.5+8.5+32+20+12;
if isFirstTimeLoaded{
variableHeights?.insert(0.0, at: indexPath.item)
}
var cellHeight : CGFloat = 20+48+11+14+10+14.5+14.5+11.5+32+13
if let userName = posts[indexPath.item].user_name{
let userNameRect = NSString(string:userName).boundingRect(with: CGSize(width:view.frame.width,height:200), options: NSStringDrawingOptions.usesFontLeading.union(NSStringDrawingOptions.usesLineFragmentOrigin), attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 16)], context: nil)
cellHeight = cellHeight + userNameRect.height
}
if let postTitle = posts[indexPath.item].post_text_title?.html2String{
let titleRect = NSString(string:postTitle).boundingRect(with: CGSize(width:view.frame.width,height:1000), options: NSStringDrawingOptions.usesFontLeading.union(NSStringDrawingOptions.usesLineFragmentOrigin), attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 16)], context: nil)
cellHeight = cellHeight+titleRect.height
}
if posts[indexPath.item].post_text != nil{
cellHeight = cellHeight+(variableHeights?[indexPath.row])!
}
if posts[indexPath.item].post_img_url != nil{
let contentType = posts[indexPath.item].post_content_type!
switch contentType {
case "EXTERNAL_LINK":
fallthrough
case "EXTERNAL_VIDEO":
cellHeight = cellHeight+300
break
default:
let postImageUrl=posts[indexPath.item].post_img_url!
let imageUrl = URL(string: postImageUrl)
do{
let imageData = try Data(contentsOf: imageUrl!)
let image = UIImage(data: imageData)
if image != nil{
cellHeight = cellHeight + image!.size.height
}
}
catch let error {
print("ERROR : \(error.localizedDescription)")
}
}
}
return CGSize(width:view.frame.width,height:cellHeight)
}
func calculateVariableSize(tag:Int,variableSize:CGFloat,indexPath:IndexPath){
isFirstTimeLoaded = false
print("variable size \(String(describing: variableHeights?.count))")
if variableHeights?[tag] == 0.0{
variableHeights?[tag]=variableSize
//collectionView.reloadSections(IndexSet.init())
collectionView.reloadItems(at: [indexPath])
}
我通过了委托以根据Web视图的内容调整单元格的大小,但是在收到委托后,它看起来像(单元格相互重叠)
这里有什么帮助吗?
删除文本html标签的底部约束Give(Leading,Trailing,top and fix height and select heightContraint make height Contraint> =)U应该为文本HTML计算字符串的高度
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
//Calculate the height of the string text
return size + TextHtmlHeight
}
然后单元格将根据文本高度增加
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.