Slide 1

Slide 1 text

UITableViewͱͷ Swiftyͳ ෇͖߹͍ํΛ ߟ͑ͯΈΔ by takasek 2016/8/29 AKIBA.swift×SwiftѪ޷ձ

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

UITableViewΛ ੍͢Δऀ͸ iOSΛ੍͢

Slide 5

Slide 5 text

ͱݴ͍͍ͬͯ΄ͲʹTableView͸ΞϓϦ։ൃͷ͋ ΒΏΔ৔໘ͰݱΕ·͢ ͔͠͠ɺ ౕ(TableView)͸ݹͷCocoaͷࢧ഑Լʹ͋Δ … ͳΜͱ͔Swiftyͳੈքʹ Ҿ͖ͣΓग़ͤͳ͍ͩΖ͏͔…!? ͱ͍͏࿩Λ͠·͢ɻ

Slide 6

Slide 6 text

UITableViewͷ ࢖͍ํͷ͓͞Β͍

Slide 7

Slide 7 text

1. cellΛregister͢Δ reuseIdentifierʹɺCellͷΫϥεΛඥ෇͚Δ 4 registerClass Cell͕ίʔυͰ࡞੒͞Ε͍ͯΔ৔߹ 4 registerNib Cell͕xibͰ࡞੒͞Ε͍ͯΔ৔߹ ௚઀IBͰPrototype Cellsͱͯ͠σβΠϯͨ͠cell Ͱ͋Ε͹ɺ໌ࣔతͳregister͸ෆཁ

Slide 8

Slide 8 text

2. UITableViewDataSourceΛ࣮૷ 4 numberOfSectionsInTableView(tableView:) 4 tableView(tableView:numberOfRowsInSecti on:) 4 tableView(tableView:cellForRowAtIndexPa th indexPath:) 4 etc

Slide 9

Slide 9 text

3. ͋ͱ͸͓೚ͤ tableView͕ɺ 4 ηΫγϣϯͷ਺ΛٻΊɺ 4 ηΫγϣϯ಺ͷߦ਺ΛٻΊɺ 4 ֤CellΛΠϯελϯεԽ͋Δ͍͸࠶ར༻͠ɺ 4 ద੾ʹϨΠΞ΢τ͠ɺ 4 දࣔʂ

Slide 10

Slide 10 text

TableViewपΓͷ໾ׂΛ෼͚ͯߟ͑ Α͏ 4 σʔλιʔεͷத਎ͷ؅ཧ 4 indexPathͰಛఆ͞ΕΔ 4 ڵຯ͕͋Δͷ͸σʔλ 4 dequeue͢΂͖Cellͷ࣮ମͷ؅ཧ 4 reuseIdentifierͰಛఆ͞ΕΔ 4 ڵຯ͕͋Δͷ͸දࣔ

Slide 11

Slide 11 text

σʔλιʔεͷத਎ͷ؅ཧ (indexPathͰಛఆ)

Slide 12

Slide 12 text

ૉͷ··ͩͱԿ͕ࠔΔ͔ let data = dataSource[indexPath.row] // AnyObject 4 σʔλͷܕ͕෼͔Βͳ͍ 4 σʔλΛ໢ཏͰ͖ͳ͍ 4 σʔλΛArrayʹవΊΔऔ͔͔ͬΓ͕ͳ͍ 4 શͯͷܕΛڞ௨͢Δϓϩτίϧʹద߹ͤ͞ Δͱ͍͏ͷ΋ͳʔ… 4 ʮ͜ͷtableViewͰ࢖͏ܕʯΛ໢ཏ͍ͨ͠

Slide 13

Slide 13 text

໢ཏ͍ͨ͠ʁ

Slide 14

Slide 14 text

ྻڍ͍ͨ͠ʂ

Slide 15

Slide 15 text

Swiftyʹղܾ ஋͖ͭEnumͷArrayΛ࡞Δ

Slide 16

Slide 16 text

enum CellViewModel { case Color(UIColor) case Switch(Bool) case Slider(Float) } let datasource: [CellViewModel] = [ .Color(.redColor()), .Switch(true), .Slider(1.0), ] func tableView( tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath ) -> UITableViewCell { switch datasource[indexPath.row] { case .Color(let color): ... case .Switch(let on): ... case .Slider(let value): ... } }

Slide 17

Slide 17 text

͜͏͢Ε͹ 4 ܕ͸ਖ਼֬ʹࢦఆͰ͖Δ 4 ͲΜͳछྨͷσʔλΛ࢖͏ͷ͔Ұ໨ྎવ 4 ໢ཏతʹॲཧΛهड़Ͱ͖Δ …ͱɺྑ͍͜ͱͣ͘ΊͰ͢ʂ

Slide 18

Slide 18 text

දࣔ͢ΔCellͷ࣮ମͷ؅ཧ (reuseIdentifierͰಛఆ)

Slide 19

Slide 19 text

ૉͷ··ͩͱԿ͕ࠔΔ͔ 4 reuseͯ͠ฦͬͯ͘Δͷ͸UITableViewCell 4 Cellͷܕ͕෼͔Βͳ͍ 4 ໌ࣔతʹμ΢ϯΩϟετ͢Δͷ͸ܕਪଌͷ Ըܙʹ͔͋ͣΕͣ൵͍͠ 4 identifierΛจࣈϦςϥϧͰࢦఆ͠ͳ͚Ε͹ͳ Βͳ͍ͷͰɺෆ҆

Slide 20

Slide 20 text

Swiftyʹղܾ େମͷ৔߹͸identifierͱΫϥε໊͸ಉ͡ͰOK dequeueͷࡍʹ͸ɺidentifierͷ୅ΘΓʹ UITableViewCell.Type Λ౉ͯ͠ɺฦͬͯ͘Δܕ ͸ UITableViewCell ʹڧ੍Ωϟετ͢Δ

Slide 21

Slide 21 text

func registerNib( sameNibNameAndIdentifierAs cellClass: Cell.Type) { registerNib( UINib(nibName: "\(cellClass)", bundle: nil), forCellReuseIdentifier: "\(cellClass)") } func registerClass( sameIdentifierAs cellClass: Cell.Type) { registerClass(cellClass, forCellReuseIdentifier: "\(cellClass)") } func dequeue( sameIdentifierAs cellClass: Cell.Type, forIndexPath indexPath: NSIndexPath) -> Cell { return dequeueReusableCellWithIdentifier( "\(cellClass)", forIndexPath: indexPath ) as! Cell }

Slide 22

Slide 22 text

ʮCell໊ͱidentifier͸ಉ͡ʹ͢Δʯ ͱϧʔϧΛܾΊΔ͚ͩͰɺ ϦςϥϧͰͷidentifier͕ ࢦఆෆཁʹʂ

Slide 23

Slide 23 text

ʢ๫࿦ʣ

Slide 24

Slide 24 text

൓࿦1 func dequeue( sameIdentifierAs cellClass: Cell.Type, forIndexPath indexPath: NSIndexPath ) -> Cell { let identifier = "\(cellClass)" return dequeueReusableCellWithIdentifier( identifier, forIndexPath: indexPath ) as! Cell } ʮ as! Cell ͸ཚ๫͗ͩ͢Ζ!?ʯ

Slide 25

Slide 25 text

ʢਖ਼࿦ʣ

Slide 26

Slide 26 text

͔͠͠ɺ 4 identifier͕register͞Ε͍ͯͳ͚Ε͹ɺΩϟετ͢Δલʹ UIKit࣌఺ͰΫϥογϡ͢ΔʢiOS6Ҏ߱ʣ 4 register͞Εͨܕ͸identifierͱಉҰͩͱอূͰ͖Δ 4 identifier͸ಛఆͷCellͷܕʹඥ෇͍͍ͯΔ 4 identifier͕ผͷܕ͕ඥ෇͍͍ͯͨͱͨ͠ΒɺͦΕ͸ identifierͷࢦఆ͕ෆຯ͍ ͱߟ͑Ε͹ɺ ! Ͱ໰୊͋Γ·ͤΜ dequeueReusableCellWithIdentifier( identifier: String, forIndexPath indexPath: NSIndexPath) -> UITableViewCell // newer dequeue method // guarantees a cell is returned and resized properly, // assuming identifier is registered

Slide 27

Slide 27 text

൓࿦2 let identifier = "\(cellClass)" ʮidentifier͕cellClassͱಉ͡ͱ͸ݶΒͳ͍ͩ Ζ!?ʯ

Slide 28

Slide 28 text

…ͦ͏ͳΜͰ͢ΑͶʔ

Slide 29

Slide 29 text

࣮͸ɺCellͷΫϥεͱIdentifier͸1:1ؔ܎Ͱ͸ͳ͍ ಉ͡Hogeͱ͍͏ॲཧΛ͢Δ͕֎؍͕ҧ͏Cell͸͜ ͏͍ͨ͠ 4 RedHogeCell.xib 4 class: HogeCell 4 identifier: "RedHoge" 4 BlueHogeCell.xib 4 class: HogeCell 4 identifier: "BlueHoge"

Slide 30

Slide 30 text

ਏ͍

Slide 31

Slide 31 text

ؒʹstructΛט·ͤΔͱ͔ɺͦΕΛGenericʹ͠ ͯΈΔͱ͔ɺ৭ʑߟ͑ͯ͸Έ·ͨ͠ Ͱ΋ɺͪΐͬͱຊ࣭తͰ͸ͳ͍෦෼Ͱ࣮૷͕๲Β Έ͗͢ΔͷͰඍົͳײ͡ 4 ଟ͘ͷ৔߹͸Cellͱidentifier͕1:1ͰࡁΉ 4 ͦ͏Ͱͳ͍৔߹͸identifierΛϦςϥϧͰࢦఆ Ͱ͖ΔΑ͏OptionalͳҾ਺Λ࣋ͨͤɺటष͘ ख࡞ۀͰඥ෇͚Δ …ͱ͍͏ͷ͕མͱ͠ॴ͔ͳͱ

Slide 32

Slide 32 text

·ͱΊ ᶃTableView͕σʔλʹؔ৺͕͋Δέʔε ɹ= indexPathͰಛఆ 4 σʔλͷछྨΛ஋͖ͭEnumͰ໢ཏˍܕ҆શʹऔΓѻ͏ ᶄTableView͕Cellͷ࣮ମʹؔ৺͕͋Δέʔε ɹ= reuseIdentifierͰಛఆ 4 reuseIdentifier = Class໊ ͱ͍͏ϧʔϧΛಋೖ͠ɺ ڧ੍ ΩϟετͰແཧ໼ཧܕ҆શʹऔΓѻ͏ 4 ࣮ࡍ͸reuseIdentifier:Class͸ଟର1ͳͷͰɺOptionalͰ identifierΛࢦఆՄೳʹ

Slide 33

Slide 33 text

͜ΕΒͷ࢓૊ΈΛऔΓೖΕͨ DEMO!