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

Easy create custom design cell #273

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ class BasicExampleViewController: UIViewController,UITableViewDataSource,UITable
return self.numberOfItems
}

public func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "cell", at: index)
public func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> UICollectionViewCell {
let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "cell", at: index) as! FSPagerViewCell
cell.imageView?.image = UIImage(named: self.imageNames[index])
cell.imageView?.contentMode = .scaleAspectFill
cell.imageView?.clipsToBounds = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ class PageControlExampleViewController: UIViewController,UITableViewDataSource,U
return self.imageNames.count
}

public func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "cell", at: index)
public func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> UICollectionViewCell {
let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "cell", at: index) as! FSPagerViewCell
cell.imageView?.image = UIImage(named: self.imageNames[index])
cell.imageView?.contentMode = .scaleAspectFill
return cell
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ class TransformerExampleViewController: UIViewController,FSPagerViewDataSource,F
return imageNames.count
}

public func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "cell", at: index)
public func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> UICollectionViewCell {
let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "cell", at: index) as! FSPagerViewCell
cell.imageView?.image = UIImage(named: self.imageNames[index])
cell.imageView?.contentMode = .scaleAspectFill
cell.imageView?.clipsToBounds = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,9 @@ - (NSInteger)numberOfItemsInPagerView:(FSPagerView *)pagerView
return self.numberOfItems;
}

- (FSPagerViewCell *)pagerView:(FSPagerView *)pagerView cellForItemAtIndex:(NSInteger)index
- (UICollectionViewCell *)pagerView:(FSPagerView *)pagerView cellForItemAtIndex:(NSInteger)index
{
FSPagerViewCell *cell = [pagerView dequeueReusableCellWithReuseIdentifier:@"cell" atIndex:index];
FSPagerViewCell *cell = (FSPagerViewCell *)[pagerView dequeueReusableCellWithReuseIdentifier:@"cell" atIndex:index];
cell.imageView.image = [UIImage imageNamed:self.imageNames[index]];
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
cell.imageView.clipsToBounds = YES;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ - (NSInteger)numberOfItemsInPagerView:(FSPagerView *)pagerView
return self.imageNames.count;
}

- (FSPagerViewCell *)pagerView:(FSPagerView *)pagerView cellForItemAtIndex:(NSInteger)index
- (UICollectionViewCell *)pagerView:(FSPagerView *)pagerView cellForItemAtIndex:(NSInteger)index
{
FSPagerViewCell *cell = [pagerView dequeueReusableCellWithReuseIdentifier:@"cell" atIndex:index];
FSPagerViewCell *cell = (FSPagerViewCell *)[pagerView dequeueReusableCellWithReuseIdentifier:@"cell" atIndex:index];
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
cell.imageView.image = [UIImage imageNamed:self.imageNames[index]];
return cell;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ - (NSInteger)numberOfItemsInPagerView:(FSPagerView *)pagerView
return self.imageNames.count;
}

- (FSPagerViewCell *)pagerView:(FSPagerView *)pagerView cellForItemAtIndex:(NSInteger)index
- (UICollectionViewCell *)pagerView:(FSPagerView *)pagerView cellForItemAtIndex:(NSInteger)index
{
FSPagerViewCell * cell = [pagerView dequeueReusableCellWithReuseIdentifier:@"cell" atIndex:index];
FSPagerViewCell * cell = (FSPagerViewCell *)[pagerView dequeueReusableCellWithReuseIdentifier:@"cell" atIndex:index];
cell.imageView.image = [UIImage imageNamed:self.imageNames[index]];
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
cell.imageView.clipsToBounds = YES;
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,8 @@ public func numberOfItems(in pagerView: FSPagerView) -> Int {
return numberOfItems
}

public func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "cell", at: index)
public func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> UICollectionViewCell {
let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "cell", at: index) as! FSPagerViewCell
cell.imageView?.image = ...
cell.textLabel?.text = ...
return cell
Expand Down Expand Up @@ -348,14 +348,14 @@ func pagerView(_ pagerView: FSPagerView, didSelectItemAt index: Int)
---

```swift
func pagerView(_ pagerView: FSPagerView, willDisplay cell: FSPagerViewCell, forItemAt index: Int)
func pagerView(_ pagerView: FSPagerView, willDisplay cell: UICollectionViewCell, forItemAt index: Int)
```
> Tells the delegate that the specified cell is about to be displayed in the pager view.

---

```swift
func pagerView(_ pagerView: FSPagerView, didEndDisplaying cell: FSPagerViewCell, forItemAt index: Int)
func pagerView(_ pagerView: FSPagerView, didEndDisplaying cell: UICollectionViewCell, forItemAt index: Int)
```
> Tells the delegate that the specified cell was removed from the pager view.

Expand Down
27 changes: 12 additions & 15 deletions Sources/FSPagerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public protocol FSPagerViewDataSource: NSObjectProtocol {

/// Asks your data source object for the cell that corresponds to the specified item in the pager view.
@objc(pagerView:cellForItemAtIndex:)
func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell
func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> UICollectionViewCell

}

Expand All @@ -46,11 +46,11 @@ public protocol FSPagerViewDelegate: NSObjectProtocol {

/// Tells the delegate that the specified cell is about to be displayed in the pager view.
@objc(pagerView:willDisplayCell:forItemAtIndex:)
optional func pagerView(_ pagerView: FSPagerView, willDisplay cell: FSPagerViewCell, forItemAt index: Int)
optional func pagerView(_ pagerView: FSPagerView, willDisplay cell: UICollectionViewCell, forItemAt index: Int)

/// Tells the delegate that the specified cell was removed from the pager view.
@objc(pagerView:didEndDisplayingCell:forItemAtIndex:)
optional func pagerView(_ pagerView: FSPagerView, didEndDisplaying cell: FSPagerViewCell, forItemAt index: Int)
optional func pagerView(_ pagerView: FSPagerView, didEndDisplaying cell: UICollectionViewCell, forItemAt index: Int)

/// Tells the delegate when the pager view is about to start scrolling the content.
@objc(pagerViewWillBeginDragging:)
Expand Down Expand Up @@ -379,15 +379,15 @@ open class FSPagerView: UIView,UICollectionViewDataSource,UICollectionViewDelega
return
}
let index = indexPath.item % self.numberOfItems
function(self,cell as! FSPagerViewCell,index)
function(self,cell,index)
}

public func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
guard let function = self.delegate?.pagerView(_:didEndDisplaying:forItemAt:) else {
return
}
let index = indexPath.item % self.numberOfItems
function(self,cell as! FSPagerViewCell,index)
function(self,cell,index)
}

public func scrollViewDidScroll(_ scrollView: UIScrollView) {
Expand Down Expand Up @@ -451,7 +451,7 @@ open class FSPagerView: UIView,UICollectionViewDataSource,UICollectionViewDelega
/// Register a nib file for use in creating new pager view cells.
///
/// - Parameters:
/// - nib: The nib object containing the cell object. The nib file must contain only one top-level object and that object must be of the type FSPagerViewCell.
/// - nib: The nib object containing the cell object. The nib file must contain only one top-level object and that object must be of the type UICollectionViewCell.
/// - 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) {
Expand All @@ -463,15 +463,12 @@ open class FSPagerView: UIView,UICollectionViewDataSource,UICollectionViewDelega
/// - Parameters:
/// - identifier: The reuse identifier for the specified cell. This parameter must not be nil.
/// - index: The index specifying the location of the cell.
/// - Returns: A valid FSPagerViewCell object.
/// - Returns: A valid UICollectionViewCell object.
@objc(dequeueReusableCellWithReuseIdentifier:atIndex:)
open func dequeueReusableCell(withReuseIdentifier identifier: String, at index: Int) -> FSPagerViewCell {
open func dequeueReusableCell(withReuseIdentifier identifier: String, at index: Int) -> UICollectionViewCell {
let indexPath = IndexPath(item: index, section: self.dequeingSection)
let cell = self.collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath)
guard cell.isKind(of: FSPagerViewCell.self) else {
fatalError("Cell class must be subclass of FSPagerViewCell")
}
return cell as! FSPagerViewCell
return cell
}

/// Reloads all of the data for the collection view.
Expand Down Expand Up @@ -532,7 +529,7 @@ open class FSPagerView: UIView,UICollectionViewDataSource,UICollectionViewDelega
/// - Parameter cell: The cell object whose index you want.
/// - Returns: The index of the cell or NSNotFound if the specified cell is not in the pager view.
@objc(indexForCell:)
open func index(for cell: FSPagerViewCell) -> Int {
open func index(for cell: UICollectionViewCell) -> Int {
guard let indexPath = self.collectionView.indexPath(for: cell) else {
return NSNotFound
}
Expand All @@ -544,9 +541,9 @@ open class FSPagerView: UIView,UICollectionViewDataSource,UICollectionViewDelega
/// - Parameter index: The index that specifies the position of the cell.
/// - Returns: The cell object at the corresponding position or nil if the cell is not visible or index is out of range.
@objc(cellForItemAtIndex:)
open func cellForItem(at index: Int) -> FSPagerViewCell? {
open func cellForItem(at index: Int) -> UICollectionViewCell? {
let indexPath = self.nearbyIndexPath(for: index)
return self.collectionView.cellForItem(at: indexPath) as? FSPagerViewCell
return self.collectionView.cellForItem(at: indexPath)
}

// MARK: - Private functions
Expand Down