我遇到一個奇怪的錯誤,當我想設定estimatedItemSize的屬性UICollectionView來UICollectionViewFlowLayout.automaticSize并展示在同一時間段標題。
使用以下代碼,應用程式可以UICollectionView正確顯示。然而,一旦用戶在 上滾動UICollectionView,應用程式就會崩潰并遞回呼叫函式updateVisibleCellsNow。
我覺得從另一個StackOverflow的問題,通過設定一種解決方法解決方案estimatedItemSize,從automaticSize到none。但是,我想將自動布局功能設定為 ,UICollectionViewCell而不是自己計算單元格高度。有沒有更好的解決辦法?謝謝你。

這是我關于 ViewController 的代碼
import UIKit
class DemoCollectionViewController: UIViewController {
lazy private var collectionView: UICollectionView = { [weak self] in
guard let strongSelf = self else { return UICollectionView() }
// Setup of `UICollectionViewFlowLayout`
let flowLayout = UICollectionViewFlowLayout()
// ********* This is the line that causes crash *********
flowLayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
// ******************************************************
flowLayout.headerReferenceSize = CGSize(width: getScreenWidth(), height: 44.0)
flowLayout.sectionHeadersPinToVisibleBounds = true
// Setup of `UICollectionView`
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout).disableFrameToAutoLayout()
collectionView.dataSource = strongSelf
collectionView.register(ProfileThumbnailCollectionViewCell.self, forCellWithReuseIdentifier: "cellId")
collectionView.register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "headerId")
return collectionView
}()
override func viewDidLoad() {
super.viewDidLoad()
// Setup of the UICollectionView
view.addSubview(collectionView)
NSLayoutConstraint.activate([
collectionView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
collectionView.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor),
collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
collectionView.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor),
])
}
}
extension DemoCollectionViewController: UICollectionViewDataSource {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 10
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 20
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! ProfileThumbnailCollectionViewCell
cell.contentView.backgroundColor = .yellow
return cell
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionView.elementKindSectionHeader:
let supplementaryView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "headerId", for: indexPath)
supplementaryView.backgroundColor = .red
return supplementaryView
default:
return UICollectionReusableView()
}
}
}
這是我的代碼ProfileThumbnailCollectionViewCell:
class ProfileThumbnailCollectionViewCell: UICollectionViewCell {
private let profileThumbnailImageView: UIImageView = {
let profileThumbnailImageView = UIImageView()
profileThumbnailImageView.translatesAutoresizingMaskIntoConstraints = false
profileThumbnailImageView.backgroundColor = .red
profileThumbnailImageView.layer.cornerRadius = 60
profileThumbnailImageView.layer.masksToBounds = true
return profileThumbnailImageView
}()
private let editPenButton: UIButton = {
let editPenButton = UIButton()
editPenButton.translatesAutoresizingMaskIntoConstraints = false
editPenButton.backgroundColor = .mainGreen
editPenButton.layer.cornerRadius = 16
editPenButton.layer.masksToBounds = true
return editPenButton
}()
override init(frame: CGRect) {
super.init(frame: frame)
// Content View
contentView.backgroundColor = .yellow
NSLayoutConstraint.activate([
contentView.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width)
])
// Profile Thumbnail ImageView
contentView.addSubview(profileThumbnailImageView)
NSLayoutConstraint.activate([
profileThumbnailImageView.centerXAnchor.constraint(equalTo: contentView.centerXAnchor),
profileThumbnailImageView.widthAnchor.constraint(equalToConstant: 120),
profileThumbnailImageView.heightAnchor.constraint(equalToConstant: 120),
profileThumbnailImageView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 25),
profileThumbnailImageView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -25)
])
// Edit thumbnail pen ImageView
contentView.addSubview(editPenButton)
NSLayoutConstraint.activate([
editPenButton.widthAnchor.constraint(equalToConstant: 32),
editPenButton.heightAnchor.constraint(equalToConstant: 32),
editPenButton.rightAnchor.constraint(equalTo: contentView.rightAnchor),
editPenButton.rightAnchor.constraint(equalTo: contentView.rightAnchor)
])
}
required init?(coder: NSCoder) {
fatalError("This class should be inited from code instead of a nib file; init(coder:) has not been implemented")
}
}
uj5u.com熱心網友回復:
在 ViewDidLoad 中呼叫 registerNib
func registerNib(){
let nib = UINib(nibName: "ProfileThumbnailCollectionViewCell", bundle: nil)
self.collectionView.register(nib, forCellWithReuseIdentifier: "ProfileThumbnailCollectionViewCell")
}
uj5u.com熱心網友回復:
經過幾個小時的調查,我意外地找到了一個變通的解決方案,但這肯定不應該被視為最終的解決方案。
class ProfileThumbnailCollectionViewCell: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
// Content View
contentView.backgroundColor = .yellow
// ******** This is the weird point ********
let targetCellWidth = UIScreen.main.bounds.width - 1
NSLayoutConstraint.activate([
contentView.widthAnchor.constraint(equalToConstant: targetCellWidth)
])
}
}
在UICollectionView不拋出任何錯誤,并同時顯示在標題視圖和UICollectionView正確。
如果您可以找到有關這種情況的任何想法,請在此處留下您的評論。非常感謝您的資訊!謝謝!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/396582.html
標籤:ios 迅速 用户界面 uicollectionviewcell uicollectionviewflowlayout
上一篇:使用“PHPickerViewController”選擇視頻時是否可以限制視頻時長?
下一篇:如何列印NSValue位元組
