Reputation: 61
If we give the low number of items in a dataSource it won't cause the hanging the issue. If we give more than 10000+ data you can see the hanging issue.
import UIComponents
class TextViewCell: UICollectionViewCell {
let textView: UITextView = {
let tv = UITextView()
tv.isScrollEnabled = false
tv.translatesAutoresizingMaskIntoConstraints = false
return tv
}()
override init(frame: CGRect) {
super.init(frame: frame)
contentView.addSubview(textView)
textView.pin()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// Configure the cell with text
func configure(with text: String) {
textView.text = text
}
}
func createLayout() -> UICollectionViewCompositionalLayout {
let itemSize = NSCollectionLayoutSize(
widthDimension: .fractionalWidth(1.0),
heightDimension: .estimated(50)
)
let groupSize = NSCollectionLayoutSize(
widthDimension: .fractionalWidth(1.0),
heightDimension: .estimated(50)
)
let item = NSCollectionLayoutItem(layoutSize: itemSize)
let group = NSCollectionLayoutGroup.vertical(layoutSize: groupSize, subitems: [item])
let section = NSCollectionLayoutSection(group: group)
return .init(section: section)
}
class ViewController: UIViewController, UICollectionViewDataSource {
var collectionView: UICollectionView!
let texts = [
"Short text",
"This is a longer text that should increase the height of the cell.This is a longer text that should increase the height of the cell.This is a longer text that should increase the height of the cell.",
"Even longer text that might span multiple lines and should show how well the estimated height works.Even longer text that might span multiple lines and should show how well the estimated height works.Even longer text that might span multiple lines and should show how well the estimated height works.Even longer text that might span multiple lines and should show how well the estimated height works.Even longer text that might span multiple lines and should show how well the estimated height works.Even longer text that might span multiple lines and should show how well the estimated height works.Even longer text that might span multiple lines and should show how well the estimated height works.Even longer text that might span multiple lines and should show how well the estimated height works.Even longer text that might span multiple lines and should show how well the estimated height works.",
]
override func viewDidLoad() {
super.viewDidLoad()
// Create layout
let layout = createLayout()
// Initialize collection view
collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: layout)
collectionView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
collectionView.backgroundColor = .white
collectionView.dataSource = self
// Register cell
collectionView.register(TextViewCell.self, forCellWithReuseIdentifier: "TextViewCell")
view.addSubview(collectionView)
}
// DataSource methods
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return texts.count * 10000
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TextViewCell", for: indexPath) as! TextViewCell
let textIndex = indexPath.item % texts.count
let text = texts[textIndex]
cell.configure(with: text)
return cell
}
}
This is the sample code you can run and see where the issue is caused. I think It layout the cell attributes for the each cell when scrolling. it will cause the issue only for estimated height with collectionview compositional layout. Please anyone help me out for this problem.
Thanks in advance devs.
Upvotes: 0
Views: 46