在我的 iPad 應用程式中,我UICollectionView在UITableViewCell. 我的問題是加載 tableView 時,集合視圖單元格未正確調整,但只要我旋轉或更改螢屏方向,布局就會正確調整。
這是我在啟動時得到的:

我正在使用 collectionView 的寬度來計算這些單元格的寬度,但我認為在加載 tableView 時,tableView 單元格內的 collectionView 沒有正確的布局。這是我在啟動時也想要的旋轉應用程式后的正確布局:

這是我在 main 中的主要cellForRowAt方法:tableViewViewController
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell", for: indexPath) as! CustomTableViewCell
cell.layoutIfNeeded()
// To invalidate layout when screen orientation changes
if let layout = cell.collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
layout.invalidateLayout()
}
cell.heighCollectionView.constant = cell.collectionView.collectionViewLayout.collectionViewContentSize.height
return cell
}
當螢屏方向改變時,我只是重新加載 tableView:
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
這就是我如何計算 CollectionView 單元格的布局:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if traitCollection.horizontalSizeClass == .compact {
let width = self.collectionView.frame.size.width
let height = width / 2
return CGSize(width: width, height: height)
}
else {
let width = (self.collectionView.frame.size.width - 10) / 2
let height = width / 2
return CGSize(width: width, height: height)
}
}
在CustomTableViewCell子類中awakeFromNib(),我只是設定 collectionView 的委托和資料源:
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
collectionView.delegate = self
collectionView.dataSource = self
}
我的問題僅在加載 View Controller 時出現,如何collectionView在首次加載時設定正確的布局?
如果您需要查看完整代碼,這里是 repo 的鏈接:https ://github.com/shwaitkumar/RotateCollectionViewInsideTableViewCell.git
uj5u.com熱心網友回復:
當initViewController 時,它獲取高度的默認值,而不是您計算的高度。只需viewDidAppear重新加載 tableView 的資料即可計算高度。
順便說一句,func tableView(_ tableView:, cellForRowAt indexPath:)僅用于回傳單元格,因此請洗掉那里的計算高度。
代碼會是這樣的
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
tableView.delegate = self
tableView.dataSource = self
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.tableView.reloadData()
}
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell", for: indexPath) as! CustomTableViewCell
//cell.layoutIfNeeded() // removed
// To invalidate layout when screen orientation changes
if let layout = cell.collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
layout.invalidateLayout()
}
//cell.heighCollectionView.constant = cell.collectionView.collectionViewLayout.collectionViewContentSize.height // removed
return cell
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/536529.html
