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
Post
Replies
Boosts
Views
Activity
It doesn't appear to come from the image as I reproduce the issue with a single label in my CollectionViewCell
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)
}
}
Well, it looks like not using the storyboard removes the issue in my test project but not in my real project. 😟
Thanks for your answer but the cell il empty, so the issue doesn't come from the content being duplicated. Plus looking at the view hierarchy I can see that the cell is is indeed duplicated and not its content.
Tanks to the people who mentioned Sherlock.
After quitting the Sherlock app it started working properly again!
I think the issue comes from the fact I was using a deprecated version.
Hey,
It goes away, but I cannot do async work when tapping the button. (The real-world application is to call an async func at this point)
Does that mean it is impossible to use async functions pre-iOS 15 in swiftUI ?
Great news, It looks like issue is fixed on XCode 14.3 ! :D