Share
Embed
「文化を調和させる」 をやってみる Try “Blending Culture”
■ Special Thanks To:
- Daniel H Steinberg
Twitter(https://twitter.com/dimsumthinking)
Web Site(http://dimsumthinking.com/)
■ Sample repo
- https://github.com/masashi-sutou/BlendingCulturesExample
Masashi-Sutou
March 11, 2016
Transcript
107 slides
-
ʮจԽΛௐͤ͞Δʯͱʁ w USZ 4XJGUͷ̍ʹߦΘΕͨ %BOJFM)4UFJOCFSHࢯͷൃද ʮ#MFOEJOH$VMUVSFʯͷ͜ͱ w 4XJGUΒ͍͠.7$Ϟσϧͷ 5BCMF7JFX$POUSPMMFSͷ࡞Γ ํΛհ͍ͯͨ͠
w USZ4XJGU͔ͩΒɺ5SZ l#MFOEJOH$VMUVSFzͯ͜ͱ ͰɺΛࢥ͍ग़ͯࣗ͠Ͱ ͬͯΈΔ Ҿ༻ݩɿIUUQEFWDMBTTNFUIPEKQTNBSUQIPOFUSZTXJGUTUEBZQN -
– Daniel H Steinberg l4XJGUͰΞϓϦέʔγϣϯΛॻ͘ͱ͍͏͜ͱ0CKFDUJWF$ Ͱॻ͔ΕͨΞϓϦέʔγϣϯΛͨͩ4XJGUʹॻ͖͑Δ͚ͩ Ͱͳ͘ɺ4XJGUͷಛֶΛड͚ೖΕΔඞཁ͕͋Γ·͢ɻ ͜ͷߨԋͰɺඪ४తͳ.7$Ͱߏ͞ΕͨςʔϒϧϏϡʔ Λ༻ͨ͠ΞϓϦέʔγϣϯΛ4XJGUʹॻ͖͑Δͱ͜Ζ͔ Β࢝·ΓɺΑΓ4XJGUΒ͍͠ίʔυʹ͢ΔͨΊʹɺؔܕϓ
ϩάϥϛϯάɺΦϒδΣΫτࢦϓϩάϥϛϯάɺσβΠϯ ύλʔϯɺϓϩτίϧࢦϓϩάϥϛϯάͷߟ͑ํΛద༻͠ ͍͖ͯ·͢ɻz ʮจԽΛௐͤ͞Δʯͱʁ -
ௐ͍ͤͨ͞จԽͨͪ w 0010CKFDU0SJFOUFE1SPHSBNNJOH ΦϒδΣΫτ ࢦϓϩάϥϛϯά w 1011SPUPDPM0SJFOUFE1SPHSBNNJOH ϓϩτίϧࢦ ϓϩάϥϛϯά
w '1'VODUJPOBM1SPHSBNNJOH ϑΝϯΫγϣφϧϓϩ άϥϛϯά w .7$.PEFM7JFX$POUSPMMFS ϞσϧɾϏϡʔɾίϯ τϩʔϥʔ -
'JSTU%FNP w USZ4XJGUͷൃදͰ %FDL $BSEΫϥεͷιʔε ެ։͞Εͣ w ಠࣗͰߟ࣮͑ͯ͠Έͨਓ͕ 2JJUBʹߘ͍ͯͨ͠ͷͰࢀߟ ʹͨ͠
w IUUQRJJUBDPNTV@LJUFNT DBFGBBGF -
)BOE7$TXJGU खࡳͷՃมߋલ import UIKit class HandVC: UITableViewController { private let
hand = Hand() override func viewDidLoad() { super.viewDidLoad() self.navigationItem.leftBarButtonItem = editButtonItem() } @IBAction private func addNewCard(sender: UIBarButtonItem) { if hand.numberOfCards < 5 { hand.addNewCardAtIndex(0) tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0)], withRowAnimation: .Fade) } } -
)BOE7$TXJGU खࡳͷՃมߋޙ import UIKit class HandVC: UITableViewController { private
let hand = Hand() override func viewDidLoad() { super.viewDidLoad() self.navigationItem.leftBarButtonItem = editButtonItem() } @IBAction private func addNewCard(sender: UIBarButtonItem) { if hand.numberOfCards < 5 { hand.addNewCardAtIndex(0) insertTopRow() } } private func insertTopRow() { tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0)], withRowAnimation: .Fade) } -
)BOE7$TXJGU खࡳͷຕͱදࣔ // MARK: - Table view data source
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return hand.numberOfCards } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier(“cardCell", forIndexPath: indexPath) let card = hand.cardAtPosition(indexPath.row) cell.textLabel?.text = card.rank.description cell.textLabel?.textColor = card.color cell.detailTextLabel?.text = card.suit.description return cell } -
)BOE7$TXJGU खࡳͷআมߋલ override func tableView(tableView: UITableView, commitEditingStyle editingStyle:UITableViewCellEditingStyle, forRowAtIndexPath
indexPath: NSIndexPath) { if editingStyle == .Delete { hand.deleteCardAtIndex(indexPath.row) tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) } } -
)BOE7$TXJGU खࡳͷআมߋޙ override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle,
forRowAtIndexPath indexPath: NSIndexPath) { if editingStyle == .Delete { hand.deleteCardAtIndex(indexPath.row) deleteRowAtIndexPath(indexPath) } } private func deleteRowAtIndexPath(indexPath: NSIndexPath) { tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) } -
)BOE7$TXJGU खࡳͷҠಈ override func tableView(tableView: UITableView moveRowAtIndexPath sourceIndexPath: NSIndexPath,
toIndexPath destinationIndexPath: NSIndexPath) { hand.moveCard(sourceIndexPath.row, toIndex: destinationIndexPath.row) } -
)BOETXJGU खࡳͷϞσϧΫϥε import UIKit class Hand { private let
deck = Deck() private var cards = [Card]() var numberOfCards: Int { return cards.count } func cardAtPosition(index: Int) -> Card { return cards[index] } -
)BOETXJGU खࡳͷϞσϧΫϥεՃɾআɾҠಈ func addNewCardAtIndex(index: Int) { insertCard(deck.nextCard(), atIndex: index)
} private func insertCard(card: Card, atIndex index: Int) { cards.insert(card, atIndex: index) } func deleteCardAtIndex(index: Int) { cards.removeAtIndex(index) } func moveCard(fromIndex: Int, toIndex: Int) { let cardToMove = cards[fromIndex] deleteCardAtIndex(fromIndex) insertCard(cardToMove, atIndex: toIndex) } -
)BOETXJGU खࡳͷϞσϧΫϥεՃɾআɾҠಈ func addNewCardAtIndex(index: Int) { insertCard(deck.nextCard(), atIndex: index)
} private func insertCard(card: Card, atIndex index: Int) { cards.insert(card, atIndex: index) } func deleteCardAtIndex(index: Int) { cards.removeAtIndex(index) } func moveCard(fromIndex: Int, toIndex: Int) { let cardToMove = cards[fromIndex] deleteCardAtIndex(fromIndex) insertCard(cardToMove, atIndex: toIndex) } -
)BOETXJGU खࡳͷϞσϧߏମ import UIKit struct Hand { private let
deck = Deck() private var cards = [Card]() var numberOfCards: Int { return cards.count } func cardAtPosition(index: Int) -> Card { return cards[index] } -
4XJGUͷDMBTTͱTUSVDUͷҧ͍ w l.PEFM$MBTTͰͳ͘ 7BMVF5ZQFʹ͠·͢z%BOJFM )4UFJOCFSH w খͯ͘͞୯७ͳσʔλΛΧϓ ηϧԽ͍ͨ͠߹ w ࢀর͠ΑΓ͠
ίϐʔ ͕ ͍͍߹ w ܧঝ͕ඞཁͳ͍߹ http://qiita.com/mini_house/items/22b7b88794b9f8a32381 -
)BOETXJGU खࡳͷϞσϧߏମՃɾআɾҠಈ mutating func addNewCardAtIndex(index: Int) { insertCard(deck.nextCard(), atIndex:
index) } private mutating func insertCard(card: Card, atIndex index: Int) { cards.insert(card, atIndex: index) } mutating func deleteCardAtIndex(index: Int) { cards.removeAtIndex(index) } mutating func moveCard(fromIndex: Int, toIndex: Int) { let cardToMove = cards[fromIndex] deleteCardAtIndex(fromIndex) insertCard(cardToMove, atIndex: toIndex) } -
)BOE7$TXJGU ϞσϧͷએݴΛఆ͔Βม import UIKit class HandVC: UITableViewController { private var
hand = Hand() override func viewDidLoad() { super.viewDidLoad() self.navigationItem.leftBarButtonItem = editButtonItem() } @IBAction private func addNewCard(sender: UIBarButtonItem) { if hand.numberOfCards < 5 { hand.addNewCardAtIndex(0) tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0)], withRowAnimation: .Fade) } } -
)BOETXJGU खࡳͷߏମՃͷNVUBUJOHΛ֎͢ mutating func addNewCardAtIndex(index: Int) { insertCard(deck.nextCard(), atIndex:
index) } func addNewCardAtIndex(index: Int) -> Hand { return insertCard(deck.nextCard(), atIndex: index) } -
)BOETXJGU खࡳͷߏମՃͰΠϯελϯεΛฦ͢ private func insertCard(card: Card, atIndex index: Int)
-> Hand { var mutableCards = cards mutableCards.insert(card, atIndex: index) return Hand(deck: deck, cards: mutableCards) } -
)BOETXJGU खࡳͷߏମՃͰΠϯελϯεΛฦ͢ private func insertCard(card: Card, atIndex index: Int)
-> Hand { var mutableCards = cards mutableCards.insert(card, atIndex: index) return Hand(deck: deck, cards: mutableCards) } -
)BOETXJGU खࡳͷϞσϧΫϥε import UIKit struct Hand { private var
deck = Deck() private var cards = [Card]() var numberOfCards: Int { return cards.count } func cardAtPosition(index: Int) -> Card { return cards[index] } -
)BOETXJGU खࡳͷϞσϧΫϥε import UIKit struct Hand { private let
deck = Deck() private var cards = [Card]() init() { } private init (deck: Deck, cards: [Card]) { self.cards = cards } var numberOfCards: Int { return cards.count } -
)BOETXJGU खࡳͷߏମՃͰΠϯελϯεΛฦ͢ private func insertCard(card: Card, atIndex index: Int)
-> Hand { var mutableCards = cards mutableCards.insert(card, atIndex: index) return Hand(deck: deck, cards: mutableCards) } -
)BOE7$TXJGU खࡳͷՃΠϯελϯεΛड͚औΔ import UIKit class HandVC: UITableViewController { private
var hand = Hand() override func viewDidLoad() { super.viewDidLoad() self.navigationItem.leftBarButtonItem = editButtonItem() } @IBAction private func addNewCard(sender: UIBarButtonItem) { if hand.numberOfCards < 5 { hand = hand.addNewCardAtIndex(0) insertTopRow() } } -
)BOE7$TXJGU खࡳͷআͱҠಈՃͱಉ༷ʹ͍ͨ͠ override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle,
forRowAtIndexPath indexPath: NSIndexPath) { if editingStyle == .Delete { hand = hand.deleteCardAtIndex(indexPath.row) deleteRowAtIndexPath(indexPath) } } override func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) { hand = hand.moveCard(sourceIndexPath.row, toIndex: destinationIndexPath.row) } -
)BOETXJGU खࡳͷߏମআͰΠϯελϯεΛฦ͢ mutating func deleteCardAtIndex(index: Int) { cards.removeAtIndex(index) }
func deleteCardAtIndex(index: Int) -> Hand { var mutableCards = cards mutableCards.removeAtIndex(index) return Hand(deck: deck, cards: mutableCards) } -
)BOETXJGU खࡳͷߏମҠಈͰΠϯελϯεΛฦ͢ func moveCard(fromIndex: Int, toIndex: Int) -> Hand
{ return deleteCardAtIndex(fromIndex) .insertCard(cards[fromIndex], atIndex: toIndex) } mutating func moveCard(fromIndex: Int, toIndex: Int) { let cardToMove = cards[fromIndex] deleteCardAtIndex(fromIndex) insertCard(cardToMove, atIndex: toIndex) } -
$BSE$FMMTXJGU खࡳΛදࣔ͢ΔΧελϜηϧʢ7JFXʣ import UIKit class CardCell: UITableViewCell { @IBOutlet
private weak var suitLabel: UILabel! @IBOutlet private weak var rankLabel: UILabel! override func awakeFromNib() { super.awakeFromNib() } func fillWith(card: Card) { rankLabel.textColor = card.color rankLabel.text = card.rank.description suitLabel.text = card.suit.description } } -
)BOE7$TXJGU खࡳΛදࣔ͢Δίϯτϩʔϥʔ override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
-> UITableViewCell { guard let cell = tableView .dequeueReusableCellWithIdentifier("cardCell", forIndexPath: indexPath) as? CardCell else { fatalError("Could not create CardCell") } cell.fillWith(hand.cardAtPosition(indexPath.row)) return cell } -
HVBSEMFUFMTFͷಛ w 4XJGU͔ΒՃ͞ΕͨɺͷόϦσʔγϣϯνΣοΫΛॻ͖͘͢͢Δߏจ w ΞϯϥοϓͱOJMνΣοΫΛಉ࣌ʹߦ͑ɺΞϯϥοϓ͞ΕͨมHVBSEจΛൈ͚ͨޙͰ ༻Մ w HVBSEจͷFMTFઅͷதͰɺSFUVSO CSFBL DPOUJOVF
UISPXͷͲΕ͔ΛߦΘͳ͍ͱί ϯύΠϧΤϥʔ http://qiita.com/mishimay/items/75fb0958f33079ff0e8a func show(message: String?) { guard let theMessage = message else { return } print(theMessage) // Ξϯϥοϓͨ͠มΛ͏͜ͱ͕Ͱ͖Δ } -
)BOE7$TXJGU खࡳΛදࣔ͢Δίϯτϩʔϥʔ override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
-> UITableViewCell { guard let cell = tableView .dequeueReusableCellWithIdentifier("cardCell", forIndexPath: indexPath) as? CardCell else { fatalError("Could not create CardCell") } cell.fillWith(hand.cardAtPosition(indexPath.row)) return cell } -
TVCTDSJQUΛ͏ w ྻࣙॻͷཁૉʹΞΫηε͢ΔॲཧΛɺΫϥεߏମʹࣗͰఆٛͰ͖Δ w ࠓճɺߏମʹͨ͠)BOEͷ$BSETͷཁૉʹΞΫηε͢ΔॲཧΛTVCTDSJQUͰه ड़͢Δ struct Hand {
private let deck = Deck() private var cards = [Card]() var numberOfCards: Int { return cards.count } subscript(index: Int) -> Card { return cards[index] } -
)BOE7$TXJGU खࡳΛදࣔ͢Δίϯτϩʔϥʔ override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
-> UITableViewCell { guard let cell = tableView .dequeueReusableCellWithIdentifier("cardCell", forIndexPath: indexPath) as? CardCell else { fatalError("Could not create CardCell") } cell.fillWith(hand.cardAtPosition(indexPath.row)) return cell } cell.fillWith(hand[indexPath.row]) return cell } -
%BUB4PVSDFTXJGU 7$͔ΒϞσϧґଘͷॲཧΛग़͢ import UIKit class DataSource: NSObject, UITableViewDataSource {
private var hand = Hand() func addItemTo(tableView: UITableView) {} func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {} func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {} func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {} func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {} func insertTopRowIn(tableView: UITableView) {} func deleteRowAtIndexPath(indexPath: NSIndexPath, from tableView: UITableView) {} } -
)BOE7$TXJGU 7$ͰEBUB4PVSDFΛࢦఆ͢Δ import UIKit class HandVC: UITableViewController { private
var dataSource = DataSource() override func viewDidLoad() { super.viewDidLoad() tableView.dataSource = dataSource self.navigationItem.leftBarButtonItem = editButtonItem() } @IBAction private func addNewCard(sender: UIBarButtonItem) { dataSource.addItemTo(tableView) } } -
%BUB4PVSDFTXJGU 7$͔ΒϞσϧґଘͷॲཧΛग़͢ import UIKit class DataSource: NSObject, UITableViewDataSource {
private var hand = Hand() func addItemTo(tableView: UITableView) {} func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {} func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {} func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {} func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {} func insertTopRowIn(tableView: UITableView) {} func deleteRowAtIndexPath(indexPath: NSIndexPath, from tableView: UITableView) {} } -
4PVSDF5ZQFTXJGU ϓϩτίϧͰҾ্͖͛Δ import UIKit protocol SourceType: UITableViewDataSource { func
insertTopRowIn(tableView: UITableView) func deleteRowAtIndexPath(indexPath: NSIndexPath, from tableView: UITableView) } extension SourceType { func insertTopRowIn(tableView: UITableView) { tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0)], withRowAnimation: .Fade) } func deleteRowAtIndexPath(indexPath: NSIndexPath, from tableView: UITableView) { tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) } } -
%BUB4PVSDFTXJGU %BUB4PVSDFΛ4PVSDF5ZQFʹ४ڌ import UIKit class DataSource: NSObject, UITableViewDataSource, SourceType
{ private var hand = Hand() func addItemTo(tableView: UITableView) {} func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {} func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {} func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {} func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {} } -
QSPUPDPMFYUFOTJPOͱʁ w QSPUPDPMʢΠϯλϑΣʔεͷఆٛʣΛ֦ு͠ɺϝιουͷ࣮ΛՃ͢Δػೳ w ศརͳQSPUPDPMFYUFOTJPO͕ͩɺ֨ೲܕϓϩύςΟΛ࣋ͭ͜ͱ͕Ͱ͖ͳ͍ protocol MyProtocol { func
hoge() } extension MyProtocol { func hoge() { print("hoge") } } class MyClass: MyProtocol { } let myInstance = MyClass() myInstance.hoge() // hoge ͱग़ྗ͞ΕΔ -
)BOETXJGU %BUB5ZQFҾ্͖͛Δલʹ import UIKit struct Hand { private let
deck = Deck() private var cards = [Card]() init() {} private init (deck: Deck, cards: [Card]) {} subscript(index: Int) -> Card {} var numberOfCards: Int {} func addNewCardAtIndex(index: Int) -> Hand {} private func insertCard(card: Card, atIndex index: Int) -> Hand {} func deleteCardAtIndex(index: Int) -> Hand {} func moveCard(fromIndex: Int, toIndex: Int) -> Hand {} } -
)BOETXJGU %BUB5ZQFҾ্͖͛Δલʹ import UIKit struct Hand { private let
deck = Deck() private var cards = [Card]() init() {} private init (deck: Deck, cards: [Card]) {} subscript(index: Int) -> Card {} var numberOfItems: Int {} func addNewItemAtIndex(index: Int) -> Hand {} private func insertItem(card: Card, atIndex index: Int) -> Hand {} func deleteItemAtIndex(index: Int) -> Hand {} func moveItem(fromIndex: Int, toIndex: Int) -> Hand {} } -
%BUB5ZQFTXJGU %BUB5ZQFҾ্͖͛Δ import UIKit protocol DataType { var numberOfItems:
Int { get } func addNewItemAtIndex(index: Int) -> Self func deleteItemAtIndex(index: Int) -> Self func moveItem(fromIndex: Int, toIndex: Int) -> Self } -
%BUB5ZQFTXJGU %BUB5ZQFʹ४ڌͨ͠ͷΛฦ͢ import UIKit protocol DataType { var numberOfItems:
Int { get } func addNewItemAtIndex(index: Int) -> Self func deleteItemAtIndex(index: Int) -> Self func moveItem(fromIndex: Int, toIndex: Int) -> Self } -
)BOETXJGU )BOE%BUB5ZQFʹ४ڌ import UIKit struct Hand : DataType {
private let deck = Deck() private var cards = [Card]() init() {} private init (deck: Deck, cards: [Card]) {} subscript(index: Int) -> Card {} var numberOfItems: Int {} func addNewItemAtIndex(index: Int) -> Hand {} private func insertItem(card: Card, atIndex index: Int) -> Hand {} func deleteItemAtIndex(index: Int) -> Hand {} func moveItem(fromIndex: Int, toIndex: Int) -> Hand {} } -
4PVSDF5ZQFTXJGU %BUB5ZQFΛϓϩύςΟʹએݴ import UIKit protocol SourceType: UITableViewDataSource { var
dataObject: DataType { get set } func insertTopRowIn(tableView: UITableView) func deleteRowAtIndexPath(indexPath: NSIndexPath, from tableView: UITableView) } extension SourceType { func insertTopRowIn(tableView: UITableView) { tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0)], withRowAnimation: .Fade) } func deleteRowAtIndexPath(indexPath: NSIndexPath, from tableView: UITableView) { tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) } } -
%BUB4PVSDFTXJGU )BOEΛ%BUB5ZQFͰड͚Δ import UIKit class DataSource: NSObject, UITableViewDataSource, SourceType
{ var dataObject: DataType = Hand() func addItemTo(tableView: UITableView) { if dataObject.numberOfItems < 5 { dataObject = dataObject.addNewItemAtIndex(0) insertTopRowIn(tableView) } } } -
%BUB4PVSDFTXJGU )BOEHVBSEͰ߹Θͤͯఆ͢Δ func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->
UITableViewCell { guard let cell = tableView .dequeueReusableCellWithIdentifier("cardCell", forIndexPath: indexPath) as? CardCell, hand = dataObject as? Hand else { fatalError("Could not create CardCell or Hand instance") } cell.fillWith(hand[indexPath.row]) return cell } -
%BUB4PVSDFTXJGU import UIKit class DataSource: NSObject, UITableViewDataSource, SourceType {
var dataObject: DataType = Hand() func addItemTo(tableView: UITableView) { if dataObject.numberOfItems < 5 { dataObject = dataObject.addNewItemAtIndex(0) insertTopRowIn(tableView) } } -
4PVSDF5ZQFTXJGU 4PVSDF5ZQFʹఆΛ࣋ͨͤΔ import UIKit protocol SourceType: UITableViewDataSource { var
dataObject: DataType { get set } var conditionForAdding: Bool { get } func insertTopRowIn(tableView: UITableView) func deleteRowAtIndexPath(indexPath: NSIndexPath, from tableView: UITableView) } extension SourceType { func insertTopRowIn(tableView: UITableView) { tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0)], withRowAnimation: .Fade) } func deleteRowAtIndexPath(indexPath: NSIndexPath, from tableView: UITableView) { tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) } } -
4PVSDF5ZQFTXJGU 4PVSDF5ZQFʹఆΛ࣋ͨͤΔ import UIKit protocol SourceType: UITableViewDataSource { var
dataObject: DataType { get set } var conditionForAdding: Bool { get } func insertTopRowIn(tableView: UITableView) func deleteRowAtIndexPath(indexPath: NSIndexPath, from tableView: UITableView) } extension SourceType { func addItemTo(tableView: UITableView) { if conditionForAdding { dataObject = dataObject.addNewItemAtIndex(0) insertTopRowIn(tableView) } } func insertTopRowIn(tableView: UITableView) { tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0)], withRowAnimation: .Fade) } func deleteRowAtIndexPath(indexPath: NSIndexPath, from tableView: UITableView) { tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) } } -
%BUB4PVSDFTXJGU ͷఆͷΈߦ͏ import UIKit class DataSource: NSObject, UITableViewDataSource, SourceType
{ var dataObject: DataType = Hand() var conditionForAdding :Bool { return dataObject.numberOfItems < 5 } import UIKit class DataSource: NSObject, UITableViewDataSource, SourceType { var dataObject: DataType = Hand() func addItemTo(tableView: UITableView) { if dataObject.numberOfItems < 5 { dataObject = dataObject.addNewItemAtIndex(0) insertTopRowIn(tableView) } } -
)BOE%BUB4PVSDFTXJGU %BUB4PVSDFͷαϒΫϥε࡞ import UIKit class HandDataSource: DataSource { init()
{ super.init(dataObject: Hand()) } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { guard let cell = tableView .dequeueReusableCellWithIdentifier("cardCell", forIndexPath: indexPath) as? CardCell, hand = dataObject as? Hand else { return UITableViewCell() } cell.fillWith(hand[indexPath.row]) return cell } override var conditionForAdding: Bool { return dataObject.numberOfItems < 5 } } -
)BOE%BUB4PVSDFTXJGU %BUB4PVSDFͷαϒΫϥε࡞ import UIKit class HandDataSource: DataSource { init()
{ super.init(dataObject: Hand()) } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { guard let cell = tableView .dequeueReusableCellWithIdentifier("cardCell", forIndexPath: indexPath) as? CardCell, hand = dataObject as? Hand else { return UITableViewCell() } cell.fillWith(hand[indexPath.row]) return cell } override var conditionForAdding: Bool { return dataObject.numberOfItems < 5 } } -
%BUB4PVSDFTXJGU %BUB4PVSDF͔ΒϞσϧґଘΛͳ͘͢ import UIKit class DataSource: NSObject, UITableViewDataSource, SourceType
{ var dataObject: DataType var conditionForAdding :Bool { return false } import UIKit class DataSource: NSObject, UITableViewDataSource, SourceType { var dataObject: DataType = Hand() var conditionForAdding :Bool { return dataObject.numberOfItems < 5 } -
%BUB4PVSDFTXJGU %BUB4PVSDFͷΠχγϟϥΠβ࡞ import UIKit class DataSource: NSObject, UITableViewDataSource, SourceType
{ var dataObject: DataType var conditionForAdding :Bool { return false } init<A: DataType>(dataObject: A) { self.dataObject = dataObject } -
)BOE7$TXJGU 7$Ͱ)BOE%BUB4PVSDFΛࢦఆ import UIKit class HandVC: UITableViewController { private
var dataSource = HandDataSource() override func viewDidLoad() { super.viewDidLoad() tableView.dataSource = dataSource self.navigationItem.leftBarButtonItem = editButtonItem() } @IBAction private func addNewCard(sender: UIBarButtonItem) { dataSource.addItemTo(tableView) } } -
5SZl#MFOEJOH$VMUVSFzΛͬ ͯΈͨײ w ൃද࣌ؒʹΑΓɺΉͳ͘লུ͞Εͯ͠·ͬͨ෦ ͕͋ΔͷʹͬͯΈͯؾͮ͘ɻ w ͳͷͰɺࣗͰखΛಈ͔͠ͳ͕Β࣮ࡍʹͬͯΈΔ ͜ͱΛڧ͓͘קΊ͢Δ w αϯϓϧͰͳ͘ɺ͏গ͠ػೳͷଟ͍ϓϩδΣΫ
τʹಋೖ͢ΔͱɺͲͷΑ͏ʹιʔε͕มԽ͢Δͷ͔ɺ ·ͩมԽͤ͟ΔΛಘͳ͍ͷ͔ɺUSZͯ͠Έ͍ͨɻ