Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix scrollToItem call fatalError when out of range #354

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions Sources/FSPagerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ open class FSPagerView: UIView,UICollectionViewDataSource,UICollectionViewDelega
internal var numberOfItems: Int = 0
internal var numberOfSections: Int = 0

internal var identifier: String = ""

fileprivate var dequeingSection = 0
fileprivate var centermostIndexPath: IndexPath {
guard self.numberOfItems > 0, self.collectionView.contentSize != .zero else {
Expand Down Expand Up @@ -332,7 +334,9 @@ open class FSPagerView: UIView,UICollectionViewDataSource,UICollectionViewDelega
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let index = indexPath.item
self.dequeingSection = indexPath.section
let cell = self.dataSource!.pagerView(self, cellForItemAt: index)
guard let cell = self.dataSource?.pagerView(self, cellForItemAt: index) else {
return self.collectionView.dequeueReusableCell(withReuseIdentifier: self.identifier, for: indexPath)
}
return cell
}

Expand Down Expand Up @@ -445,6 +449,7 @@ open class FSPagerView: UIView,UICollectionViewDataSource,UICollectionViewDelega
/// - identifier: The reuse identifier to associate with the specified class. This parameter must not be nil and must not be an empty string.
@objc(registerClass:forCellWithReuseIdentifier:)
open func register(_ cellClass: Swift.AnyClass?, forCellWithReuseIdentifier identifier: String) {
self.identifier = identifier
self.collectionView.register(cellClass, forCellWithReuseIdentifier: identifier)
}

Expand All @@ -455,6 +460,7 @@ open class FSPagerView: UIView,UICollectionViewDataSource,UICollectionViewDelega
/// - identifier: The reuse identifier to associate with the specified nib file. This parameter must not be nil and must not be an empty string.
@objc(registerNib:forCellWithReuseIdentifier:)
open func register(_ nib: UINib?, forCellWithReuseIdentifier identifier: String) {
self.identifier = identifier
self.collectionView.register(nib, forCellWithReuseIdentifier: identifier)
}

Expand Down Expand Up @@ -511,9 +517,8 @@ open class FSPagerView: UIView,UICollectionViewDataSource,UICollectionViewDelega
/// - animated: Specify true to animate the scrolling behavior or false to adjust the pager view’s visible content immediately.
@objc(scrollToItemAtIndex:animated:)
open func scrollToItem(at index: Int, animated: Bool) {
guard index < self.numberOfItems else {
fatalError("index \(index) is out of range [0...\(self.numberOfItems-1)]")
}
guard self.numberOfItems > 0, index >= 0 else { return }
let index = min(index, self.numberOfItems - 1)
let indexPath = { () -> IndexPath in
if let indexPath = self.possibleTargetingIndexPath, indexPath.item == index {
defer {
Expand Down