我有一个UITableView
,在UITableViewCell
里面是一个UICollectionView
(水平滚动)。
UICollectionViewCell
将根据标签上的内容在点击时扩展,此逻辑是通过在UILable
内使用设置为UICollectionViewCell
的2或0的 numberOfLine 属性来完成的
UICollectionView
得到了完美的扩展,但是UITableViewCell
的高度却没有按照UICollectionView
的内容高度扩展。
我选中此答案
UICollectionView inside a UITableViewCell -- dynamic height?
我可以通过使用它来扩展UICollectionView
,但不能帮助我扩展UITableViewCell
我也尝试使用 systemLayoutSizeFittingSize 方法,但无法解决问题,
下面是代码:
// ViewController
class ViewController: UIViewController {
//MARK: -Outlet Declaration-
@IBOutlet weak var tblView: UITableView!
//MARK: -Variable Declaration-
//MARK: -ViewController Life Cycle Methods-
override func viewDidLoad() {
super.viewDidLoad()
}
//MARK: -Button Methods-
}
extension ViewController : UITableViewDelegate ,UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CellTableView", for: indexPath) as? CellTableView
return cell!
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 300
}
}
// TableViewCell
class CellTableView: UITableViewCell {
@IBOutlet weak var collectionView: UICollectionView!
//MARK: -Variable Declaration-
var isExpandCell = false
override func awakeFromNib() {
super.awakeFromNib()
setCollectionViewFlowLayout()
collectionView.delegate = self
collectionView.dataSource = self
}
func setCollectionViewFlowLayout() {
let flowLayout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
flowLayout.itemSize = UICollectionViewFlowLayout.automaticSize
flowLayout.estimatedItemSize = CGSize.init(width:UIScreen.main.bounds.width, height: 1)
}
// THIS IS THE MOST IMPORTANT METHOD
//
// This method tells the auto layout
// You cannot calculate the collectionView content size in any other place,
// because you run into race condition issues.
// NOTE: Works for iOS 8 or later
// override func systemLayoutSizeFitting(_ targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize {
// self.collectionView.frame = CGRect.init(x: 0, y: 0, width: targetSize.width, height:targetSize.height)
// return self.collectionView.collectionViewLayout.collectionViewContentSize
// }
}
extension CellTableView : UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CellCollectionView", for: indexPath) as? CellCollectionView
cell?.lblDynamicContent.numberOfLines = (isExpandCell ? 0 : 2)
cell?.lblDynamicContent.text = Constant.kDummyText
return cell!
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
isExpandCell = !isExpandCell
collectionView.reloadData()
}
}
// ColletionView单元格
class CellCollectionView: UICollectionViewCell {
//MARK: -Outlet Declaration-
@IBOutlet weak var lblDynamicContent: UILabel!
//MARK- -Override Methods-
override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
setNeedsLayout()
layoutIfNeeded()
let size = systemLayoutSizeFitting(layoutAttributes.size)
var frame = layoutAttributes.frame
frame.size.height = ceil(size.height)
layoutAttributes.frame = frame
return layoutAttributes
}
}