Thursday, September 11, 2025
HomeiOS Developmentios - Collectionview compositional structure with estimated merchandise top with giant dataSource...

ios – Collectionview compositional structure with estimated merchandise top with giant dataSource hanging difficulty


If we give the low variety of objects in a dataSource it will not trigger the hanging the difficulty. If we give greater than 10000+ information you possibly can see the hanging difficulty.

import UIComponents

class TextViewCell: UICollectionViewCell {
    
    let textView: UITextView = {
        let television = UITextView()
        television.isScrollEnabled = false
        television.translatesAutoresizingMaskIntoConstraints = false
        return television
    }()
    
    override init(body: CGRect) {
        tremendous.init(body: body)
        contentView.addSubview(textView)
        textView.pin()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been carried out")
    }
    
    // Configure the cell with textual content
    func configure(with textual content: String) {
        textView.textual content = textual content
    }
}

func createLayout() -> UICollectionViewCompositionalLayout {
    let itemSize = NSCollectionLayoutSize(
        widthDimension: .fractionalWidth(1.0),
        heightDimension: .estimated(50)
    )
    let groupSize = NSCollectionLayoutSize(
        widthDimension: .fractionalWidth(1.0),
        heightDimension: .estimated(50)
    )
    let merchandise = NSCollectionLayoutItem(layoutSize: itemSize)
    let group = NSCollectionLayoutGroup.vertical(layoutSize: groupSize, subitems: [item])
    
    let part = NSCollectionLayoutSection(group: group)
    return .init(part: part)
}

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() {
        tremendous.viewDidLoad()
        
        // Create structure
        let structure = createLayout()
        
        // Initialize assortment view
        collectionView = UICollectionView(body: view.bounds, collectionViewLayout: structure)
        collectionView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        collectionView.backgroundColor = .white
        collectionView.dataSource = self
        
        
        // Register cell
        collectionView.register(TextViewCell.self, forCellWithReuseIdentifier: "TextViewCell")
        
        view.addSubview(collectionView)
    }
    
    // DataSource strategies
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection part: Int) -> Int {
        return texts.depend * 10000
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TextViewCell", for: indexPath) as! TextViewCell
        let textIndex = indexPath.merchandise % texts.depend
        let textual content = texts[textIndex]
        cell.configure(with: textual content)
        return cell
    }
}

That is the pattern code you possibly can run and see the place the difficulty is induced. I believe It structure the cell attributes for the every cell when scrolling. it’s going to trigger the difficulty just for estimated top with collectionview compositional structure. Please anybody assist me out for this drawback.

Thanks upfront devs.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments