kishikawakatsumi / IBPCollectionViewCompositionalLayout

Backport of UICollectionViewCompositionalLayout to earlier iOS 12

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Problem with reusing cells inside nested collection views. (orthogonal scrolling)

hadyhallak opened this issue · comments

Hi 👋

when orthogonal scrolling is enabled, cells are being dequeued from the root collection view and placed in a nested collection view to achieve the orthogonal scrolling which is pretty nice.

the problem is the root collection view cannot reuse those cells anymore,, which is leading to creating unlimited number of cells when scrolling horizontally (orthogonally).

To demonstrate the issue,, I updated TextCell from Apple samples to display the current cell number.

here's the code

/*
 See LICENSE folder for this sample’s licensing information.
 
 Abstract:
 Generic text cell
 */

import UIKit

class TextCell: UICollectionViewCell {
    static var i = 1
    
    let label = UILabel()
    static let reuseIdentifier = "text-cell-reuse-identifier"
    let index: Int
    
    override init(frame: CGRect) {
        index = TextCell.i
        TextCell.i += 1
        
        super.init(frame: frame)
        configure()
    }
    required init?(coder: NSCoder) {
        fatalError("not implemnted")
    }
    
}

extension TextCell {
    func configure() {
        let lbl = UILabel()
        lbl.text = "\(index)"
        lbl.translatesAutoresizingMaskIntoConstraints = false
        contentView.addSubview(lbl)
        NSLayoutConstraint.activate([
            lbl.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 8),
            lbl.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 8),
            ])
        
        label.translatesAutoresizingMaskIntoConstraints = false
        label.adjustsFontForContentSizeCategory = true
        contentView.addSubview(label)
        label.font = UIFont.preferredFont(forTextStyle: .caption1)
        let inset = CGFloat(10)
        NSLayoutConstraint.activate([
            label.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: inset),
            label.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -inset),
            label.topAnchor.constraint(equalTo: contentView.topAnchor, constant: inset),
            label.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -inset)
            ])
    }
}

` ``

Thank you for reporting! I have workaround the issue for now. Could you make sure that the problem is fixed?

Yeah thanks, that's solved the issue for now.

I noticed that you're registering and dequeuing cells from the nested collectionView now, which is pretty enough as a workaround.

But do you have any plans to improve this behavior?

I though of rewriting the reuse system in a custom UICollectionView and use that as the root collectionView. which will manage cells to be reused both inside it and across nested collection views (as iOS 13 now does). but that sounded a bit wrong to me,,, what do you think?

btw, are PRs welcome?

Thanks. I agree with you. Using a customized collection view seems overkill. Also, it makes it more complicated for the user site. So I keep this until if we find another right way. (But I don't expect much.)

are PRs welcome?

Absolutely yes! Please try it, find bugs, and send PRs! I've implemented most of the features, but I'm sure there are still lots of bugs.

Now I'm working on adding support for Swift Package Manager. The directory structure will change significantly. Probably It's good to wait to finish it before you start.