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

Remove refs to IGListSectionType, fix TV app example #676

Closed
wants to merge 2 commits into from
Closed
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 @@ -15,7 +15,7 @@
import UIKit
import IGListKit

final class CarouselSectionController: IGListSectionController, IGListSectionType {
final class CarouselSectionController: IGListSectionController {

var number: Int?

Expand All @@ -24,29 +24,29 @@ final class CarouselSectionController: IGListSectionController, IGListSectionTyp
self.inset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 10)
}

func numberOfItems() -> Int {
override func numberOfItems() -> Int {
return 1
}

func sizeForItem(at index: Int) -> CGSize {
override func sizeForItem(at index: Int) -> CGSize {
let height = collectionContext?.containerSize.height ?? 0
let aspectRatio: CGFloat = 0.75 // 3:4
let width = height * aspectRatio

return CGSize(width: width, height: height)
}

func cellForItem(at index: Int) -> UICollectionViewCell {
override func cellForItem(at index: Int) -> UICollectionViewCell {
let cell = collectionContext!.dequeueReusableCell(withNibName: "CarouselCell", bundle: nil, for: self, at: index) as! CarouselCell
let value = number ?? 0
cell.titleLabel.text = "#\(value + 1)"
return cell
}

func didUpdate(to object: Any) {
override func didUpdate(to object: Any) {
number = object as? Int
}

func didSelectItem(at index: Int) {}
override func didSelectItem(at index: Int) {}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ final class DemoItem: NSObject {

}

final class DemoSectionController: IGListSectionController, IGListSectionType {
final class DemoSectionController: IGListSectionController {

var object: DemoItem?

Expand All @@ -40,26 +40,26 @@ final class DemoSectionController: IGListSectionController, IGListSectionType {
inset = UIEdgeInsets(top: 0, left: 50, bottom: 10, right: 0)
}

func numberOfItems() -> Int {
override func numberOfItems() -> Int {
return 1
}

func sizeForItem(at index: Int) -> CGSize {
override func sizeForItem(at index: Int) -> CGSize {
let itemWidth = (collectionContext!.containerSize.width / 2) - inset.left
return CGSize(width: itemWidth, height: 100)
}

func cellForItem(at index: Int) -> UICollectionViewCell {
override func cellForItem(at index: Int) -> UICollectionViewCell {
let cell = collectionContext!.dequeueReusableCell(of: DemoCell.self, for: self, at: index) as! DemoCell
cell.label.text = object?.name
return cell
}

func didUpdate(to object: Any) {
override func didUpdate(to object: Any) {
self.object = object as? DemoItem
}

func didSelectItem(at index: Int) {
override func didSelectItem(at index: Int) {
if let identifier = object?.controllerIdentifier {
let storyboard = UIStoryboard(name: "Demo", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: identifier)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import UIKit
import IGListKit

final class HorizontalSectionController: IGListSectionController, IGListSectionType, IGListAdapterDataSource {
final class HorizontalSectionController: IGListSectionController, IGListAdapterDataSource {

var number: Int?

Expand All @@ -32,25 +32,25 @@ final class HorizontalSectionController: IGListSectionController, IGListSectionT
self.inset = UIEdgeInsets(top: 20, left: 0, bottom: 20, right: 0)
}

func numberOfItems() -> Int {
override func numberOfItems() -> Int {
return 1
}

func sizeForItem(at index: Int) -> CGSize {
override func sizeForItem(at index: Int) -> CGSize {
return CGSize(width: collectionContext!.containerSize.width, height: 340)
}

func cellForItem(at index: Int) -> UICollectionViewCell {
override func cellForItem(at index: Int) -> UICollectionViewCell {
let cell = collectionContext!.dequeueReusableCell(of: EmbeddedCollectionViewCell.self, for: self, at: index) as! EmbeddedCollectionViewCell
adapter.collectionView = cell.collectionView
return cell
}

func didUpdate(to object: Any) {
override func didUpdate(to object: Any) {
number = object as? Int
}

func didSelectItem(at index: Int) {}
override func didSelectItem(at index: Int) {}

// MARK: IGListAdapterDataSource

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,28 @@
import UIKit
import IGListKit

final class LabelSectionController: IGListSectionController, IGListSectionType {
final class LabelSectionController: IGListSectionController {

var object: String?

func numberOfItems() -> Int {
override func numberOfItems() -> Int {
return 1
}

func sizeForItem(at index: Int) -> CGSize {
override func sizeForItem(at index: Int) -> CGSize {
return CGSize(width: collectionContext!.containerSize.width, height: 55)
}

func cellForItem(at index: Int) -> UICollectionViewCell {
override func cellForItem(at index: Int) -> UICollectionViewCell {
let cell = collectionContext!.dequeueReusableCell(of: LabelCell.self, for: self, at: index) as! LabelCell
cell.label.text = object
return cell
}

func didUpdate(to object: Any) {
override func didUpdate(to object: Any) {
self.object = String(describing: object)
}

func didSelectItem(at index: Int) {}
override func didSelectItem(at index: Int) {}

}
2 changes: 1 addition & 1 deletion Guides/Best Practices and FAQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This guide provides notes and details on best practices in using `IGListKit`, ge

## Best Practices

- We recommend adding an assert to check [`-isKindOfClass:`](https://developer.apple.com/reference/objectivec/1418956-nsobject/1418511-iskindofclass) on the object you receive in [`-didUpdateToObject:`](https://instagram.github.io/IGListKit/Protocols/IGListSectionType.html#/c:objc(pl)IGListSectionType(im)didUpdateToObject:) in your section controllers.
- We recommend adding an assert to check [`-isKindOfClass:`](https://developer.apple.com/reference/objectivec/1418956-nsobject/1418511-iskindofclass) on the object you receive in [`-didUpdateToObject:`](https://github.com/Instagram/IGListKit/blob/master/Source/IGListSectionController.h#L63-L72) in your section controllers.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

once 3.0 is ready, let's re-gen docs and re-link to the docs page

This makes it easy to track down easily-overlooked mistakes in your [`IGListAdapaterDataSource`](https://instagram.github.io/IGListKit/Protocols/IGListAdapterDataSource.html#/c:objc(pl)IGListAdapterDataSource(im)listAdapter:sectionControllerForObject:) implementation.
If this assert is ever hit, that means `IGListKit` has sent your section controller the incorrect type of object.
This would only happen if your objects provide *non-unique* diff identifiers.
Expand Down
16 changes: 11 additions & 5 deletions Guides/Getting Started.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,29 @@ After installing `IGListKit`, creating a new list is easy.

### Creating a section controller

Creating a new section controller is simple. You subclass `IGListSectionController` and conform to the `IGListSectionType` protocol. Once you conform to `IGListSectionType`, the compiler will make sure you implement all of the required methods.
Creating a new section controller is simple. Subclass `IGListSectionController` and override at least `cellForItemAtIndex:` and `sizeForItemAtIndex:`.

Take a look at [LabelSectionController](https://raw.githubusercontent.com/Instagram/IGListKit/master/Examples/Examples-iOS/IGListKitExamples/SectionControllers/LabelSectionController.swift) for an example section controller that handles a `String` and configures a single cell with a `UILabel`.

```swift
class LabelSectionController: IGListSectionController, IGListSectionType {
// ...
class LabelSectionController: IGListSectionController {
override func sizeForItem(at index: Int) -> CGSize {
return CGSize(width: collectionContext!.containerSize.width, height: 55)
}

override func cellForItem(at index: Int) -> UICollectionViewCell {
return collectionContext!.dequeueReusableCell(of: MyCell.self, for: self, at: index)
}
}
```

### Creating the UI

After creating at least one section controller, you must create an `IGListCollectionView` and `IGListAdapter`.
After creating at least one section controller, you must create a `UICollectionView` and `IGListAdapter`.

```swift
let layout = UICollectionViewFlowLayout()
let collectionView = IGListCollectionView(frame: .zero, collectionViewLayout: layout)
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)

let updater = IGListAdapterUpdater()
let adapter = IGListAdapter(updater: updater, viewController: self, workingRangeSize: 0)
Expand Down