Post

Replies

Boosts

Views

Activity

Reply to UICollectionViewDiffableDataSource creating duplicate cells
Hi !I'm "Glad" I'm not alone having this issue.I created a sample project with one simple collection view and an image in the cell and I reproduce the issue.Here is the code:import UIKit import TVUIKit class AViewController: UICollectionViewController { lazy var layout = createLayout() override func viewDidLoad() { super.viewDidLoad() collectionView.setCollectionViewLayout(layout, animated: false) } override func numberOfSections(in collectionView: UICollectionView) -> Int { 10 } override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 10 } override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) } func createLayout() -> UICollectionViewLayout { UICollectionViewCompositionalLayout { (section, _) -> NSCollectionLayoutSection? in let item = NSCollectionLayoutItem( layoutSize: NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .fractionalHeight(1))) let group = NSCollectionLayoutGroup.horizontal( layoutSize: NSCollectionLayoutSize( widthDimension: .absolute(350), heightDimension: .absolute(300)), subitem: item, count: 1) let section = NSCollectionLayoutSection(group: group) section.interGroupSpacing = 50 section.orthogonalScrollingBehavior = .continuous section.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 0, bottom: 40, trailing: 0) return section } }To observe the issue you can just open the view hierarchy debugger after a few scrolls up and down and you should see the duplicated cells
Dec ’19
Reply to UICollectionViewDiffableDataSource creating duplicate cells
Alright.I created the collectionView and the cell programatically instead of using a storyboard CollectionViewController and I don't have the issue anymore 🤔. The search continues...(here is the updated code)// // ViewController.swift // testComposi // // Created by Hugo Saynac on 08/12/2019. // Copyright © 2019 Hugo Saynac. All rights reserved. // import UIKit class ColorCell: UICollectionViewCell { override init(frame: CGRect) { super.init(frame: frame) let imageView = UIImageView(image: imageLiteral(resourceName: "cat-smirk")) imageView.translatesAutoresizingMaskIntoConstraints = false addSubview(imageView) addConstraints([ imageView.leadingAnchor.constraint(equalTo: leadingAnchor), imageView.trailingAnchor.constraint(equalTo: trailingAnchor), imageView.topAnchor.constraint(equalTo: topAnchor), imageView.bottomAnchor.constraint(equalTo: bottomAnchor) ]) imageView.adjustsImageWhenAncestorFocused = true } required init?(coder aDecoder: NSCoder) { fatalError("Unavailable") } } class AViewController: UIViewController, UICollectionViewDataSource { override func viewDidLoad() { super.viewDidLoad() createCollectionView() } func numberOfSections(in collectionView: UICollectionView) -> Int { 10 } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 10 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) } fileprivate func createCollectionView() { let layout = createLayout() let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout) view.addSubview(collectionView) collectionView.translatesAutoresizingMaskIntoConstraints = false view.addConstraints([ collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor), collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor), collectionView.topAnchor.constraint(equalTo: view.topAnchor), collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor) ]) collectionView.register(ColorCell.self, forCellWithReuseIdentifier: "Cell") collectionView.dataSource = self } } func createLayout() -> UICollectionViewLayout { UICollectionViewCompositionalLayout { (section, _) -> NSCollectionLayoutSection? in let item = NSCollectionLayoutItem( layoutSize: NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .fractionalHeight(1))) let group = NSCollectionLayoutGroup.horizontal( layoutSize: NSCollectionLayoutSize( widthDimension: .absolute(350), heightDimension: .absolute(300)), subitem: item, count: 1) let section = NSCollectionLayoutSection(group: group) section.interGroupSpacing = 50 section.orthogonalScrollingBehavior = .continuous section.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 0, bottom: 40, trailing: 0) return section } } extension UIColor { static var random: UIColor { return .init(hue: .random(in: 0...1), saturation: 1, brightness: 1, alpha: 1) } }
Dec ’19