Upgrade to Pro — share decks privately, control downloads, hide ads and more …

iosdcrc-conv.pdf

C5fb6051a588c0b9dc308e97800098ad?s=47 hirose yudai
September 20, 2018
410

 iosdcrc-conv.pdf

iOSDC Reject Conference 2018 day2 で発表した内容です。

Convについて話しました

https://iosdc-reject-conference.connpass.com/event/93315/

C5fb6051a588c0b9dc308e97800098ad?s=128

hirose yudai

September 20, 2018
Tweet

Transcript

  1. J04%$3FKFDU$POGFSFODF 6*$PMMFDUJPO7JFXͷσʔλߏ଄ͱ7JFXͷݟͨ໨Λ౷Ұ͢Δ

  2. ࣗݾ঺հ *%CBOO[BJ ॴଐגࣜձࣾ"TPCJDB ৬ۀ&OHJOFFS ݺশελʔޤ৯ CBOO[BJ @CBOO[BJ@

  3. લஔ͖ IUUQTGPSUFFKQJPTEDKBQBOQSPQPTBMBBEBBBCBB ⒎FBEG

  4. $POWͱ͍͏ϥΠϒϥϦΛ࡞Γ·ͨ͠ (JU)VCϦϙδτϦ IUUQTHJUIVCDPNCBOO[BJ$POW 2JJUBʹ΋ಉ͡λΠτϧͰهࣄΛॻ͍ͨ IUUQTRJJUBDPNCBOO[BJJUFNTGECFDDBECF

  5. $POWͬͯʁ

  6. ैདྷͷॻ͖ํ public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) ->

    Int { switch section { case 0: return users.count case 1: return hobbies.count case 2: return teams.count default: fatalError() } } public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { switch (indexPath.section, indexPath.item) { case (0, _): let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ACell", for: indexPath) as! UserCell cell.configure(with: users[indexPath.item]) ... return cell case (1, _): let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "BCell", for: indexPath) as! HobbyCell cell.configure(with: hobbies[indexPath.item]) ... return cell case (2, _): let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CCell", for: indexPath) as! TeamCell cell.configure(with: teams[indexPath.item]) ... return cell default: fatalError() } }
  7. ैདྷͷॻ͖ํ public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) ->

    Int { switch section { case 0: return users.count case 1: return hobbies.count case 2: return teams.count default: fatalError() } } public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { switch (indexPath.section, indexPath.item) { case (0, _): let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ACell", for: indexPath) as! UserCell cell.configure(with: users[indexPath.item]) ... return cell case (1, _): let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "BCell", for: indexPath) as! HobbyCell cell.configure(with: hobbies[indexPath.item]) ... return cell case (2, _): let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CCell", for: indexPath) as! TeamCell cell.configure(with: teams[indexPath.item]) ... return cell default: fatalError() } } ύλʔϯϚον͠ͳ͍৔߹ͷGBUBM&SSPS
  8. ैདྷͷॻ͖ํ public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) ->

    Int { switch section { case 0: return users.count case 1: return hobbies.count case 2: return teams.count default: fatalError() } } public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { switch (indexPath.section, indexPath.item) { case (0, _): let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ACell", for: indexPath) as! UserCell cell.configure(with: users[indexPath.item]) ... return cell case (1, _): let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "BCell", for: indexPath) as! HobbyCell cell.configure(with: hobbies[indexPath.item]) ... return cell case (2, _): let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CCell", for: indexPath) as! TeamCell cell.configure(with: teams[indexPath.item]) ... return cell default: fatalError() } } ࢄΒ͹Δ'PSDFVOXSBQ
  9. ैདྷͷॻ͖ํ public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) ->

    Int { switch section { case 0: return users.count case 1: return hobbies.count case 2: return teams.count default: fatalError() } } public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { switch (indexPath.section, indexPath.item) { case (0, _): let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ACell", for: indexPath) as! UserCell cell.configure(with: users[indexPath.item]) ... return cell case (1, _): let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "BCell", for: indexPath) as! HobbyCell cell.configure(with: hobbies[indexPath.item]) ... return cell case (2, _): let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CCell", for: indexPath) as! TeamCell cell.configure(with: teams[indexPath.item]) ... return cell default: fatalError() } } ͲΕ͕ͲΕ
  10. ैདྷͷॻ͖ํ public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) ->

    Int { switch section { case 0: return users.count case 1: return hobbies.count case 2: return teams.count default: fatalError() } } public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { switch (indexPath.section, indexPath.item) { case (0, _): let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ACell", for: indexPath) as! UserCell cell.configure(with: users[indexPath.item]) ... return cell case (1, _): let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "BCell", for: indexPath) as! HobbyCell cell.configure(with: hobbies[indexPath.item]) ... return cell case (2, _): let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CCell", for: indexPath) as! TeamCell cell.configure(with: teams[indexPath.item]) ... return cell default: fatalError() } } ཁૉͷؔ܎ੑ͕ബ͍ɾԕ͍
  11. $POWΛ࢖͏ͱ

  12. collectionView .conv() .create(for: SectionType.allCases) { (sectionType, section) in // ※2

    switch sectionType { case .users: section.create(for: users) { user, item: Item<UserCell> in item.reusableIdentifier = "ACell" item.size = ... item.configureCell({ cell, _ in cell.configure(with: user) }) } case .hobbies: section.create(for: hobbies) { hobby, item: Item<ItemCell> in item.reusableIdentifier = "BCell" item.size = ... item.configureCell({ cell, _ in cell.configure(with: hobby) }) } case .teams: section.create(for: teams) { team, item: Item<ItemCell> in item.reusableIdentifier = "CCell" item.size = ... item.configureCell({ cell, _ in cell.configure(with: team) }) } } }
  13. collectionView .conv() .create(for: SectionType.allCases) { (sectionType, section) in // ※2

    switch sectionType { case .users: section.create(for: users) { user, item: Item<UserCell> in item.reusableIdentifier = "ACell" item.size = ... item.configureCell({ cell, _ in cell.configure(with: user) }) } case .hobbies: section.create(for: hobbies) { hobby, item: Item<ItemCell> in item.reusableIdentifier = "BCell" item.size = ... item.configureCell({ cell, _ in cell.configure(with: hobby) }) } case .teams: section.create(for: teams) { team, item: Item<ItemCell> in item.reusableIdentifier = "CCell" item.size = ... item.configureCell({ cell, _ in cell.configure(with: team) }) } } } ͜ΕͰ࡞Εͯ͠·͍·͢
  14. collectionView .conv() .create(for: SectionType.allCases) { (sectionType, section) in // ※2

    switch sectionType { case .users: section.create(for: users) { user, item: Item<UserCell> in item.reusableIdentifier = "ACell" item.size = ... item.configureCell({ cell, _ in cell.configure(with: user) }) } case .hobbies: section.create(for: hobbies) { hobby, item: Item<ItemCell> in item.reusableIdentifier = "BCell" item.size = ... item.configureCell({ cell, _ in cell.configure(with: hobby) }) } case .teams: section.create(for: teams) { team, item: Item<ItemCell> in item.reusableIdentifier = "CCell" item.size = ... item.configureCell({ cell, _ in cell.configure(with: team) }) } } } ͚ͩ͜͜&OVNΛҰ͚ͭͩఆٛͨ͠
  15. collectionView .conv() .create(for: SectionType.allCases) { (sectionType, section) in // ※2

    switch sectionType { case .users: section.create(for: users) { user, item: Item<UserCell> in item.reusableIdentifier = "ACell" item.size = ... item.configureCell({ cell, _ in cell.configure(with: user) }) } case .hobbies: section.create(for: hobbies) { hobby, item: Item<ItemCell> in item.reusableIdentifier = "BCell" item.size = ... item.configureCell({ cell, _ in cell.configure(with: hobby) }) } case .teams: section.create(for: teams) { team, item: Item<ItemCell> in item.reusableIdentifier = "CCell" item.size = ... item.configureCell({ cell, _ in cell.configure(with: team) }) } } } ഑ྻΛ౉ͨ͠਺͚ͩ4FDUJPO͕࡞ΒΕΔ
  16. collectionView .conv() .create(for: SectionType.allCases) { (sectionType, section) in // ※2

    switch sectionType { case .users: section.create(for: users) { user, item: Item<UserCell> in item.reusableIdentifier = "ACell" item.size = ... item.configureCell({ cell, _ in cell.configure(with: user) }) } case .hobbies: section.create(for: hobbies) { hobby, item: Item<ItemCell> in item.reusableIdentifier = "BCell" item.size = ... item.configureCell({ cell, _ in cell.configure(with: hobby) }) } case .teams: section.create(for: teams) { team, item: Item<ItemCell> in item.reusableIdentifier = "CCell" item.size = ... item.configureCell({ cell, _ in cell.configure(with: team) }) } } } ഑ྻͷཁૉ͝ͱʹ4FDUJPOΛఆٛ͢ΔͷͰ *OEFY1BUIʹΞΫηε͠ͳ͍͍ͯ͘
  17. collectionView .conv() .create(for: SectionType.allCases) { (sectionType, section) in // ※2

    switch sectionType { case .users: section.create(for: users) { user, item: Item<UserCell> in item.reusableIdentifier = "ACell" item.size = ... item.configureCell({ cell, _ in cell.configure(with: user) }) } case .hobbies: section.create(for: hobbies) { hobby, item: Item<ItemCell> in item.reusableIdentifier = "BCell" item.size = ... item.configureCell({ cell, _ in cell.configure(with: hobby) }) } case .teams: section.create(for: teams) { team, item: Item<ItemCell> in item.reusableIdentifier = "CCell" item.size = ... item.configureCell({ cell, _ in cell.configure(with: team) }) } } } 4FDUJPOʹ౉ͨ͠਺͚ͩ*UFN͕࡞ΒΕΔ
  18. collectionView .conv() .create(for: SectionType.allCases) { (sectionType, section) in // ※2

    switch sectionType { case .users: section.create(for: users) { user, item: Item<UserCell> in item.reusableIdentifier = "ACell" item.size = ... item.configureCell({ cell, _ in cell.configure(with: user) }) } case .hobbies: section.create(for: hobbies) { hobby, item: Item<ItemCell> in item.reusableIdentifier = "BCell" item.size = ... item.configureCell({ cell, _ in cell.configure(with: hobby) }) } case .teams: section.create(for: teams) { team, item: Item<ItemCell> in item.reusableIdentifier = "CCell" item.size = ... item.configureCell({ cell, _ in cell.configure(with: team) }) } } } ഑ྻͷཁૉ͝ͱʹ*UFNΛఆٛ͢ΔͷͰ *OEFY1BUIʹΞΫηε͠ͳ͍͍ͯ͘
  19. collectionView .conv() .create(for: SectionType.allCases) { (sectionType, section) in // ※2

    switch sectionType { case .users: section.create(for: users) { user, item: Item<UserCell> in item.reusableIdentifier = "ACell" item.size = ... item.configureCell({ cell, _ in cell.configure(with: user) }) } case .hobbies: section.create(for: hobbies) { hobby, item: Item<ItemCell> in item.reusableIdentifier = "BCell" item.size = ... item.configureCell({ cell, _ in cell.configure(with: hobby) }) } case .teams: section.create(for: teams) { team, item: Item<ItemCell> in item.reusableIdentifier = "CCell" item.size = ... item.configureCell({ cell, _ in cell.configure(with: team) }) } } } $FMM͝ͱͷ%BUB4PVSDFɾ%FMFHBUFͷϝιουͷϥούʔୡ
  20. collectionView .conv() .create(for: SectionType.allCases) { (sectionType, section) in // ※2

    switch sectionType { case .users: section.create(for: users) { user, item: Item<UserCell> in item.reusableIdentifier = "ACell" item.size = ... item.configureCell({ cell, _ in cell.configure(with: user) }) } case .hobbies: section.create(for: hobbies) { hobby, item: Item<ItemCell> in item.reusableIdentifier = "BCell" item.size = ... item.configureCell({ cell, _ in cell.configure(with: hobby) }) } case .teams: section.create(for: teams) { team, item: Item<ItemCell> in item.reusableIdentifier = "CCell" item.size = ... item.configureCell({ cell, _ in cell.configure(with: team) }) } } } (FOFSJDTͰΩϟετ͞Εͨ$FMM͕౉ͬͯ͘Δ
  21. collectionView .conv() .create(for: SectionType.allCases) { (sectionType, section) in // ※2

    switch sectionType { case .users: section.create(for: users) { user, item: Item<UserCell> in item.reusableIdentifier = "ACell" item.size = ... item.configureCell({ cell, _ in cell.configure(with: user) }) } case .hobbies: section.create(for: hobbies) { hobby, item: Item<ItemCell> in item.reusableIdentifier = "BCell" item.size = ... item.configureCell({ cell, _ in cell.configure(with: hobby) }) } case .teams: section.create(for: teams) { team, item: Item<ItemCell> in item.reusableIdentifier = "CCell" item.size = ... item.configureCell({ cell, _ in cell.configure(with: team) }) } } } 4FDUJPOͱ*UFNͷؔ܎ੑ͕໌֬ʹɻݶఆతʹ
  22. collectionView .conv() .create(for: SectionType.allCases) { (sectionType, section) in // ※2

    switch sectionType { case .users: section.create(for: users) { user, item: Item<UserCell> in item.reusableIdentifier = "ACell" item.size = ... item.configureCell({ cell, _ in cell.configure(with: user) }) } case .hobbies: section.create(for: hobbies) { hobby, item: Item<ItemCell> in item.reusableIdentifier = "BCell" item.size = ... item.configureCell({ cell, _ in cell.configure(with: hobby) }) } case .teams: section.create(for: teams) { team, item: Item<ItemCell> in item.reusableIdentifier = "CCell" item.size = ... item.configureCell({ cell, _ in cell.configure(with: team) }) } } } ແବͳGBUBM&SSPS͕ͳ͘ͳ͍ͬͯΔ
  23. None
  24. ͍͍͡ΌΜ

  25. ͍͍͘͢͝

  26. ࠷ߴ

  27. ࠩ෼ߋ৽ $POWͰ͸ࠩ෼ߋ৽ͷΞϧΰϦζϜͱͯ͠ 1BVM)FDLFMTBMHPSJUINΛϕʔεͱͯ͠࡞͍ͬͯ ·͢ɻͳͷͰඞཁͳ෦෼͚ͩߋ৽͢ΔͷͰແବͳॲཧ ͕ͳ͍ *(-JTU,JU͕ಉ͡ΞϧΰϦζϜ࢖͍ͬͯΔ IUUQTHJUIVCDPN*OTUBHSBN*(-JTU,JU

  28. None
  29. ४උ public protocol Differenciable { var differenceIdentifier: DifferenceIdentifier { get

    } }
  30. 4FDUJPO enum SectionType: Int { case one case two case

    three static var allCases: [SectionType] { return [.one, .two, .three] } } extension SectionType: Differenciable { var differenceIdentifier: DifferenceIdentifier { return "\(self)" } }
  31. *UFN struct ItemModel { let imageName: String var image: UIImage

    { return UIImage(named: imageName)! } } extension ItemModel: Differenciable { var differenceIdentifier: DifferenceIdentifier { return imageName } }
  32. "SSBZ let sectionTypes = SectionType.allCases let itemModels = ["forest", "moon",

    “mountain"] .map { ImageModel(imageName: $0) }
  33. $POW collectionView .conv() .create(for: sectionTypes) { (sectionType, section) in section.create(.header,

    headerOrFooter: { (header: SectionHeaderFooter<Header>) in header.reusableIdentifier = "Header" header.size = CGSize(width: UIScreen.main.bounds.width, height: 50) header.configureView { view, _ in view.nameLabel.text = "\(sectionType)".uppercased() view.nameLabel.textColor = .white view.backgroundColor = sectionType.backgroundColor } }) section.create(for: itemModels, items: { (itemModel, item: Item<Cell>) in item.reusableIdentifier = "Cell" item.sizeFor { (item, collectionView, indexPath) -> CGSize in let gridCount: CGFloat = 3 let edge = floor((collectionView.bounds.width - (gridCount - 1)) / gridCount) let size = CGSize(width: edge, height: edge) return size } item.configureCell { (cell, info) in cell.setup(with: itemModel) } item.didSelect { [weak self] (item) in let viewController = DetailViewController(imageName: itemModel.imageName) self?.navigationController?.pushViewController(viewController, animated: true) } }) }
  34. ͜ΕͰࠩ෼ߋ৽ collectionView.update()

  35. SFMPBE%BUB collectionView.reload()

  36. ͓ΘΓʹ (JU)VCϦϙδτϦ IUUQTHJUIVCDPNCBOO[BJ$POW 2JJUBʹ΋ಉ͡λΠτϧͰهࣄΛॻ͍ͨ IUUQTRJJUBDPNCBOO[BJJUFNTGECFDDBECF ελʔ͍ͩ͘͞

  37. ͓͠·͍a ?P?