• 0
Votes
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 190

Backtrace:

File: /var/www/html/thecodehelp.com/application/views/question.php
Line: 190
Function: _error_handler

File: /var/www/html/thecodehelp.com/application/controllers/Questions.php
Line: 416
Function: view

File: /var/www/html/thecodehelp.com/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Bug in UICollectionView for tvOS (not present in iOS)

I have a strange (but annoying) bug, only reproducible on a tvOS project (using tvOS 13 / xCode 11)

On tvOS, cellForItemAt is called multiple times for the same Section / Row.

When you scroll the list quickly (not 1 section a time), you will have this kind of behavior:

Bug

As you can see, the first cell of some sections have 2 or more time the same cell.

In the console, you will see:

2019-10-20 13:42:42 +0000 - [8, 0]
2019-10-20 13:42:42 +0000 - [8, 0]
2019-10-20 13:42:42 +0000 - [8, 0]

tvOS call cellForItemAt for the same cell 3 times !

This problem is not present on iOS (using the same code)

You can check with this sample project: github

Code with the bug:

import UIKit


class Cell: UICollectionViewCell {
    var lbl: UILabel!

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setup()
    }

    private func setup() {
        self.lbl = UILabel(frame: .zero)
        self.lbl.translatesAutoresizingMaskIntoConstraints = false
        self.lbl.textColor = .white
        self.contentView.addSubview(self.lbl)
        NSLayoutConstraint.activate([
            self.lbl.widthAnchor.constraint(equalTo: self.contentView.widthAnchor),
            self.lbl.widthAnchor.constraint(equalTo: self.lbl.heightAnchor),
            self.lbl.centerXAnchor.constraint(equalTo: self.contentView.centerXAnchor),
            self.lbl.centerYAnchor.constraint(equalTo: self.contentView.centerYAnchor),
        ])
    }

}

class ViewController: UICollectionViewController {

    let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

    init() {
        super.init(collectionViewLayout: Self.createLayout())
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView.isPrefetchingEnabled = false

        collectionView.register(Cell.self, forCellWithReuseIdentifier: "Cell")
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.reloadData()
    }

    private static func createLayout() -> UICollectionViewLayout {

        let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
                                              heightDimension: .fractionalHeight(1.0))
        let item = NSCollectionLayoutItem(layoutSize: itemSize)

        let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.14),
                                               heightDimension: .fractionalWidth(0.25))
        let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])
        let spacing = CGFloat(50) // 50
        group.interItemSpacing = .fixed(spacing)

        let section = NSCollectionLayoutSection(group: group)
        section.interGroupSpacing = spacing
        section.contentInsets = NSDirectionalEdgeInsets(top: 20, leading: 0, bottom: 50, trailing: 25)
        section.orthogonalScrollingBehavior = .continuous

        let layout = UICollectionViewCompositionalLayout(section: section)
        return layout
    }

    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 30
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 50
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! Cell

        if indexPath.section == 8 && indexPath.row == 0 {
            print("(Date()) - (indexPath)")
        }

        cell.backgroundColor = UIColor.blue.withAlphaComponent(0.5)
        cell.lbl.text = String((0..<10>

So it's a problem with my code or i have to open a bug report @ Apple ?