0
嗨我想設置動態單元格的高度。如何設置動態單元格的高度?
這裏是我的UICollectionViewCell
import UIKit
import Parse
import ActiveLabel
class UserShopDetailCell: UICollectionViewCell {
@IBOutlet weak var postImg: UIImageView!
@IBOutlet weak var likesLbl: UILabel!
@IBOutlet weak var usernameLbl: UILabel!
@IBOutlet weak var commentLbl: ActiveLabel!
@IBOutlet weak var descriptionLbl: ActiveLabel!
@IBOutlet weak var profileImg: UIImageView!
@IBOutlet weak var timeLbl: UILabel!
@IBOutlet weak var descriptionTop: NSLayoutConstraint!
@IBOutlet weak var commentTop: NSLayoutConstraint!
@IBOutlet weak var timeTop: NSLayoutConstraint!
override func awakeFromNib() {
}
}
,它是collectionviewcontroller
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
let cellsAcross: CGFloat = 3
let spaceBetweenCells: CGFloat = 1
let dim = (collectionView.bounds.width - (cellsAcross - 1) * spaceBetweenCells)/cellsAcross
var width = 114.0
if(isGridView==true){
return CGSize(width: 114, height: 114)
}
else {
//I want set dynamic height...not constant values
return CGSize(width: 375, height: 500)
}
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if isGridView == false {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "UserShopDetailCell", for: indexPath) as! UserShopDetailCell
... ..... ......
let basicHeight = cell.frame.size.width + 23 + cell.usernameLbl.frame.height + 1 + cell.likesLbl.frame.height
let bottomHeight = cell.timeTop.constant + cell.timeLbl.frame.height + 12
var dynamicHeight = basicHeight + bottomHeight
cell.frame.size.height = dynamicHeight
cell.sizeToFit()
cell.sizeThatFits(CGSize(width: 375, height: dynamicHeight))
cell.contentView.sizeThatFits(CGSize(width: 375, height: dynamicHeight))
return cell
}
}
正如你所看到的那樣,白色空間是500高度 - 動態高度。
我想最終從動態高度值設置單元格的高度。
但是,當我設置CGSize,然後動態高度大於CGSize時不工作。
我該如何解決這個問題?
感謝您的答覆,你可以讓我知道我如何在swift中使用? –