19.9 C
New York
Sunday, September 15, 2024

ios – Collectionview compositional structure with estimated merchandise peak with massive dataSource hanging concern


If we give the low variety of objects in a dataSource it will not trigger the hanging the problem. If we give greater than 10000+ knowledge you may see the hanging concern.

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 applied")
    }
    
    // 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.rely * 10000
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TextViewCell", for: indexPath) as! TextViewCell
        let textIndex = indexPath.merchandise % texts.rely
        let textual content = texts[textIndex]
        cell.configure(with: textual content)
        return cell
    }
}

That is the pattern code you may run and see the place the problem is brought on. I believe It structure the cell attributes for the every cell when scrolling. it can trigger the problem just for estimated peak with collectionview compositional structure. Please anybody assist me out for this downside.

Thanks prematurely devs.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles