Skip to content

Apps crashes when centeredIndexPath is being called #25

Open
@maughray

Description

@maughray

Hi,

If number of items is zero, attemp to call collectionView?.centeredIndexPath will cause "divide by zero" crash instead returning nil

Thanks.

Activity

changed the title Apps crashes if centeredIndexPath called Apps crashes when centeredIndexPath is being called on Nov 26, 2020
zykyan

zykyan commented on Mar 26, 2021

@zykyan

Any fix on this?

rcobelli

rcobelli commented on Mar 29, 2021

@rcobelli

Hi!

Can you provide some additional context around how you are getting the divide by zero crash? I've been unable to replicate.

You can see a modified version of the BaseCollectionViewController.swift from the example project below. When you load this VC in the example app then centeredIndexPath returns nil as expected.

BaseCollectionViewController.swift
override func viewDidAppear(_ animated: Bool) {
	print(self.infiniteCollectionView!.centeredIndexPath as Any)
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? Cell else {
        fatalError()
    }
    cell.update(index: self.infiniteCollectionView!.indexPath(from: indexPath).row)
    return cell
}

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

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    collectionView.scrollToItem(at: indexPath, at: self.infiniteCollectionView!.infiniteLayout.scrollDirection == .vertical ? .centeredVertically : .centeredHorizontally, animated: true)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Apps crashes when centeredIndexPath is being called · Issue #25 · arnauddorgans/InfiniteLayout