-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: Got around to adding a supplementary view source example. Fixes #153 #163 ![img_0197](https://cloud.githubusercontent.com/assets/739696/20042159/03d2489e-a443-11e6-8d60-895256b56273.PNG) - [x] All tests pass. Demo project builds and runs. - [x] I have reviewed the [contributing guide](https://github.com/Instagram/IGListKit/blob/master/CONTRIBUTING.md) Closes #169 Differential Revision: D4139910 Pulled By: rnystrom fbshipit-source-id: 846c61c4d097392f18778d46044d4989e6bdf183
- Loading branch information
Ryan Nystrom
authored and
Facebook Github Bot
committed
Nov 7, 2016
1 parent
576ab4d
commit 8fa4001
Showing
8 changed files
with
589 additions
and
336 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
Copyright (c) 2016-present, Facebook, Inc. All rights reserved. | ||
|
||
The examples provided by Facebook are for non-commercial testing and evaluation | ||
purposes only. Facebook reserves all rights not expressly granted. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | ||
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
import IGListKit | ||
|
||
final class FeedItem: IGListDiffable { | ||
|
||
let pk: Int | ||
let user: User | ||
let comments: [String] | ||
|
||
init(pk: Int, user: User, comments: [String]) { | ||
self.pk = pk | ||
self.user = user | ||
self.comments = comments | ||
} | ||
|
||
//MARK: IGListDiffable | ||
|
||
func diffIdentifier() -> NSObjectProtocol { | ||
return pk as NSObjectProtocol | ||
} | ||
|
||
func isEqual(_ object: IGListDiffable?) -> Bool { | ||
if self === object { | ||
return true | ||
} | ||
guard let object = object as? FeedItem else { return false } | ||
return user.isEqual(object.user) && comments == object.comments | ||
} | ||
|
||
} |
69 changes: 69 additions & 0 deletions
69
Example/IGListKitExamples/SectionControllers/FeedItemSectionController.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
Copyright (c) 2016-present, Facebook, Inc. All rights reserved. | ||
|
||
The examples provided by Facebook are for non-commercial testing and evaluation | ||
purposes only. Facebook reserves all rights not expressly granted. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | ||
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
import IGListKit | ||
|
||
final class FeedItemSectionController: IGListSectionController, IGListSectionType, IGListSupplementaryViewSource { | ||
|
||
var feedItem: FeedItem! | ||
|
||
override init() { | ||
super.init() | ||
supplementaryViewSource = self | ||
} | ||
|
||
// MARK: IGlistSectionType | ||
|
||
func numberOfItems() -> Int { | ||
return feedItem.comments.count | ||
} | ||
|
||
func sizeForItem(at index: Int) -> CGSize { | ||
return CGSize(width: collectionContext!.containerSize.width, height: 55) | ||
} | ||
|
||
func cellForItem(at index: Int) -> UICollectionViewCell { | ||
let cell = collectionContext?.dequeueReusableCell(of: LabelCell.self, for: self, at: index) as! LabelCell | ||
cell.label.text = feedItem.comments[index] | ||
return cell | ||
} | ||
|
||
func didUpdate(to object: Any) { | ||
feedItem = object as? FeedItem | ||
} | ||
|
||
func didSelectItem(at index: Int) {} | ||
|
||
// MARK: IGListSupplementaryViewSource | ||
|
||
func supportedElementKinds() -> [String] { | ||
return [UICollectionElementKindSectionHeader] | ||
} | ||
|
||
func viewForSupplementaryElement(ofKind elementKind: String, at index: Int) -> UICollectionReusableView { | ||
let view = collectionContext?.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, | ||
for: self, | ||
nibName: "UserHeaderView", | ||
bundle: nil, | ||
at: index) as! UserHeaderView | ||
view.handleLabel.text = "@" + feedItem.user.handle | ||
view.nameLabel.text = feedItem.user.name | ||
return view | ||
} | ||
|
||
func sizeForSupplementaryView(ofKind elementKind: String, at index: Int) -> CGSize { | ||
return CGSize(width: collectionContext!.containerSize.width, height: 40) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
Example/IGListKitExamples/ViewControllers/SupplementaryViewController.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
Copyright (c) 2016-present, Facebook, Inc. All rights reserved. | ||
|
||
The examples provided by Facebook are for non-commercial testing and evaluation | ||
purposes only. Facebook reserves all rights not expressly granted. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | ||
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
import UIKit | ||
import IGListKit | ||
|
||
final class SupplementaryViewController: UIViewController, IGListAdapterDataSource { | ||
|
||
lazy var adapter: IGListAdapter = { | ||
return IGListAdapter(updater: IGListAdapterUpdater(), viewController: self, workingRangeSize: 0) | ||
}() | ||
let collectionView = IGListCollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout()) | ||
|
||
let feedItems = [ | ||
FeedItem(pk: 1, user: User(pk: 100, name: "Jesse", handle: "jesse_squires"), comments: ["You rock!", "Hmm you sure about that?"]), | ||
FeedItem(pk: 2, user: User(pk: 101, name: "Ryan", handle: "_ryannystrom"), comments: ["lgtm", "lol", "Let's try it!"]), | ||
FeedItem(pk: 3, user: User(pk: 102, name: "Ann", handle: "abaum"), comments: ["Good luck!"]), | ||
FeedItem(pk: 4, user: User(pk: 103, name: "Phil", handle: "phil"), comments: ["yoooooooo", "What's the eta?"]), | ||
] | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
view.addSubview(collectionView) | ||
adapter.collectionView = collectionView | ||
adapter.dataSource = self | ||
} | ||
|
||
override func viewDidLayoutSubviews() { | ||
super.viewDidLayoutSubviews() | ||
collectionView.frame = view.bounds | ||
} | ||
|
||
// MARK: IGListAdapterDataSource | ||
|
||
func objects(for listAdapter: IGListAdapter) -> [IGListDiffable] { | ||
return feedItems | ||
} | ||
|
||
func listAdapter(_ listAdapter: IGListAdapter, sectionControllerFor object: Any) -> IGListSectionController { | ||
return FeedItemSectionController() | ||
} | ||
|
||
func emptyView(for listAdapter: IGListAdapter) -> UIView? { return nil } | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
Copyright (c) 2016-present, Facebook, Inc. All rights reserved. | ||
|
||
The examples provided by Facebook are for non-commercial testing and evaluation | ||
purposes only. Facebook reserves all rights not expressly granted. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | ||
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
import UIKit | ||
|
||
final class UserHeaderView: UICollectionViewCell { | ||
|
||
@IBOutlet weak var nameLabel: UILabel! | ||
@IBOutlet weak var handleLabel: UILabel! | ||
|
||
} |
Oops, something went wrong.