From d9c3eb0b55285af4b16fa2ebb274d48b8a6add93 Mon Sep 17 00:00:00 2001 From: zhengzz Date: Wed, 29 Jun 2022 10:23:37 +0800 Subject: [PATCH 1/2] Fix scrollToItem call fatalError when out of range --- Sources/FSPagerView.swift | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Sources/FSPagerView.swift b/Sources/FSPagerView.swift index 7fb17d9..0c90ddc 100644 --- a/Sources/FSPagerView.swift +++ b/Sources/FSPagerView.swift @@ -511,9 +511,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 { From 6666d1f17d40e45b08e3afbcc0edd6cc4fc000a4 Mon Sep 17 00:00:00 2001 From: zhengzz Date: Wed, 29 Jun 2022 10:40:01 +0800 Subject: [PATCH 2/2] Fix force unpacking --- Sources/FSPagerView.swift | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Sources/FSPagerView.swift b/Sources/FSPagerView.swift index 0c90ddc..23645da 100644 --- a/Sources/FSPagerView.swift +++ b/Sources/FSPagerView.swift @@ -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 { @@ -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 } @@ -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) } @@ -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) }