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

iOS アプリ作成入門

nonylene
March 13, 2016

iOS アプリ作成入門

nonylene

March 13, 2016
Tweet

More Decks by nonylene

Other Decks in Technology

Transcript

  1. J04ΞϓϦ࡞੒ೖ໳
    ,.$OPOZMFOF

    View full-size slide

  2. ͜ͷεϥΠυʹ͍ͭͯ
    w य़߹॓Ͱൃදͨ͠΋ͷ
    w "OESPJEฤʢ຅εϥΠυʣ΋͋Γ·ͪ͢͜Β

    View full-size slide

  3. ࣗݾ঺հ
    w OPOZMFOF ͷʹΕΜ

    w ,.$ೋճੜSPPU
    w (JU)VCIBUFOBFUDʜ
    w QZUIPO KBWB "OESPJE

    ,PUMJOFUDʜ

    View full-size slide

  4. J04ΞϓϦ࣮૷

    View full-size slide

  5. ࡞੒͢ΔΞϓϦ

    View full-size slide

  6. 4UBDL0WFSqPXݕࡧ

    View full-size slide

  7. ."%&*/
    )"/"4&

    View full-size slide

  8. ࢖͏"1*
    w 4UBDL0WFSqPX"1*
    w ΈΜͳେ޷͖ɺϓϩάϥϛϯάͷ2"αΠτ
    w QVCMJD"1*Ͱݕࡧ΍ৄࡉ͕औΕΔ

    View full-size slide

  9. 4UBDL0WFSqPX"1*
    w υΩϡϝϯτ
    w IUUQTBQJTUBDLFYDIBOHFDPNEPDT
    w TFBSDI"1* ྫλΠτϧʹ4XJGU

    w IUUQTBQJTUBDLFYDIBOHFDPNTFBSDI
    PSEFSEFTDTPSUBDUJWJUZJOUJUMFTXJGUTJUF
    TUBDLPWFSqPXpMUFSGQO[U%1

    View full-size slide

  10. (JU)VC
    IUUQTHJUIVCDPN
    OPOZMFOF4UBDLFSJ04

    View full-size slide

  11. 8IZJ04
    w "OESPJEΛͯ͠Δͱત·͘͠ͳΔ
    w TXJGU࠷ߴͦ͏
    w ΤϛϡϨʔλૣͦ͏
    w όʔδϣϯ໰୊ͳͦ͞͏
    w ؀ڥྑͦ͞͏
    w ྡࣳ੨໰୊

    View full-size slide

  12. ໨࣍
    w J04ͱ͸
    w ηοτΞοϓɾ)FMMP8PSME
    w ΞϓϦΛ࡞Δ
    w ײ૝

    View full-size slide

  13. J04ͱ͸
    w "QQMFͷ04
    w .BD049Λ࠶ߏ੒ͨ͠΋ͷ
    w J1IPOFJ1PEUPVDIJ1BE
    w ೔ຊʹ͓͚ΔγΣΞ͸
    w ࠷৽͸

    View full-size slide

  14. ։ൃ؀ڥ
    w 0CK$4XJGU
    w ݩʑ0CK$ͷΈ͕ͩͬͨɺ

    ೥ʹ৽ݴޠ4XJGU͕ొ৔
    w ࠓճ͸4XJGUΛ࢖͍·͢

    View full-size slide

  15. ։ൃ؀ڥ
    w 9DPEF
    w "QQMFͷ04༻ͷ*%&
    w "QQMF੡ɺNBDݶఆ
    w ࣮࣭ແྉ NBDΛങ͑͹

    View full-size slide

  16. 4XJGUͷจ๏ʢܰ͘ʣ
    class Piyo : Foo {
    var text: String?
    func makeHoge(str: String) -> Bar {
    self.text = str
    let hoge = Hoge(text: str)
    return home as! Bar
    }
    func printText() {
    print("text: " + text!)
    }
    }
    Optional

    (Nullable)
    Unwrap

    (NonNull)
    ఆ਺
    ม਺
    ֎෦Ҿ਺໊
    Ωϟετ

    View full-size slide

  17. ηοτΞοϓ

    View full-size slide

  18. ςϯϓϨʔτ
    w 4JOHMF7JFX"QQMJDBUJPOΛબ୒

    View full-size slide

  19. ໊শઃఆ
    w "OESPJEͱಉ༷4UBDLFSʹ
    w ݴޠ͸4XJGU

    View full-size slide

  20. όʔδϣϯ
    w J04͸ػछ͕"QQMF୺຤ͷΈ
    w ʢαϙʔτର৅ʣػछ͝ͱͷόʔδϣϯ͕ࠩͳ͍
    w ࠷௿όʔδϣϯΛେ͖ΊʹઃఆͰ͖Δ

    View full-size slide

  21. ΞϓϦΛىಈ
    w ୺຤Λ઀ଓɺ্ʹ͋Δl⾣z
    Λԡͯ͠ΞϓϦΛىಈ
    w J04͸ΤϛϡϨʔλ͕ૣ͍
    w ࣮ػσόοά΋ແྉԽ

    View full-size slide

  22. ॳظঢ়ଶΛோΊΔ

    View full-size slide

  23. 7JFX$POUSPMMFSTXJGU
    class ViewController: UIViewController {
    override func viewDidLoad() {
    super.viewDidLoad()
    }
    override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    }
    }
    w ը໘੍ޚ
    w ಛʹԿ΋͍ͯ͠ͳ͍

    View full-size slide

  24. .BJOTUPSZCPBSE

    View full-size slide

  25. 4UPSZ#PBSE
    w ը໘ͷભҠ΍ϨΠΞ΢τͳͲΛܾఆ͢Δ
    w جຊతʹ͸9DPEFΛ௨ͯ͡࢖͏
    w ࢖͍ํ͸ޙͰઆ໌
    w ࣮ଶ͸YNM
    w ࠓ͸ViewController͕Ұͭ͋ΔͷΈ

    View full-size slide

  26. 1SPKFDU&EJUPS
    w ϓϩδΣΫτͷઃఆ
    w Ϗϧυ
    w ໊લ
    w όʔδϣϯ
    w FUDʜ

    View full-size slide

  27. 4UPSZ#PBSEΛ։͘

    View full-size slide

  28. -BCFMΛυϥοάͯ͠഑ஔ
    https://gyazo.com/1ed1655e91611e4808c1f7b6dd71102a

    View full-size slide

  29. )FMMP 8PSMEͱهೖ

    View full-size slide

  30. ΞϓϦΛىಈ

    View full-size slide

  31. J04
    w ϨΠΞ΢τߏ੒͸͜ͷΑ͏ʹ4UPSZ#PBSEͰߦ͏
    w Ϛ΢ε͕͋Δͱศར
    w ը໘͕େ͖͍ͱศར

    View full-size slide

  32. ΞϓϦ࡞੒

    View full-size slide

  33. ϑϩʔΛߟ͑Δ
    ᶃݕࡧϫʔυΛೖྗͤ͞Δ
    ᶅ"1*Λୟ͍ͯ݁ՌΛऔಘ
    ᶅ+40/Λॲཧ
    ᶆॲཧͨ݁͠ՌΛදࣔ͢Δ

    View full-size slide

  34. ᶃݕࡧϫʔυΛೖྗ

    View full-size slide

  35. ϨΠΞ΢τ࡞੒

    View full-size slide

  36. ը໘Λ૊ΈཱͯΔ
    w ը໘ύʔπͷ͜ͱΛViewͱ͍͏
    w ೖྗཝͱϦετදࣔͷύʔπΛ্Լʹฒ΂Δ

    View full-size slide

  37. 4FBSDI#BSͷઃஔ

    View full-size slide

  38. 5BCMF7JFXͷઃஔ

    View full-size slide

  39. 5BCMF7JFXΛ޿͛Δ

    View full-size slide

  40. w 4FBSDI#BS
    w ݕࡧόʔ
    w 5BCMF7JFX
    w ಉ͡छྨͷ7JFXΛ܁Γฦ͠දࣔ͢Δ
    w ޙͰৄ͘͠આ໌
    5BCMF7JFX4FBSDI#BS

    View full-size slide

  41. 4FBSDI#BSͷઃఆ
    ೖྗώϯτ
    ςʔϚ

    View full-size slide

  42. ϓϨϏϡʔ
    w "TTJTUBOU&EJUPSΛ࢖͏

    View full-size slide

  43. ϓϨϏϡʔ
    w ӈଆදࣔʹ1SFWJFXΛબ୒

    View full-size slide

  44. 8IZ
    w ੲ͸J04͸ը໘αΠζ͕ಉͩͬͨ͡
    w ͜ͷ࣌͸αΠζݻఆͰେৎ෉ͩͬͨ
    w ࠓ͸σόΠεʹΑͬͯҟͳΔ
    ˠ ಛఆͷը໘αΠζͰௐ੔͢ΔͱଞͰ߹Θͳ͍

    View full-size slide

  45. "VUP-BZPVU
    w 7JFXͷҐஔɾαΠζΛ

    ʮ੍໿ $POTUSBJO
    ʯͰࢦఆ͢Δ
    w ࠓ·Ͱ͸ઈରతɺ͜Ε͸૬ରత

    View full-size slide

  46. "VUP-BZPVU
    w ଞʢ਌ʣͷ7JFXͱͷʜ
    w ڑ཭Λࢦఆ͢Δ
    w 9Λἧ͑Δ
    w ෯Λἧ͑Δ
    w ॎԣൺΛࢦఆ͢ΔFUDʜ

    View full-size slide

  47. "VUP-BZPVU
    w 7JFXΛબ୒ɺӈԼͷϘλϯ܈Ͱࢦఆ

    View full-size slide

  48. 4FBSDI#BSͷ੍໿
    w ্Լࠨӈͷ7JFXͱ઀͢Δ
    ੍໿ͷ௥Ճ

    View full-size slide

  49. 4FBSDI#BSͷ੍໿
    w ্Լࠨӈͷ7JFXͱ઀͢Δ

    View full-size slide

  50. 5BCMF7JFXͷ੍໿
    w Լࠨӈͷ7JFXͱ઀͢Δ
    w ্͸4FBSDI#BSͳͷͰ
    ΋͏ܾఆ͍ͯ͠Δ

    View full-size slide

  51. 5BCMF7JFXͷ੍໿
    w Լࠨӈͷ7JFXͱ઀͢Δ
    w ্͸4FBSDI#BSͳͷͰ
    ΋͏ܾఆ͍ͯ͠Δ

    View full-size slide

  52. ΞϓϦΛىಈ
    w Ϧετ෦෼͸ۭന
    w ԿΛදࣔͤ͞Δ͔Λ

    ·ܾͩΊ͍ͯͳ͍

    View full-size slide

  53. 4FBSDIˠจࣈྻऔಘ

    View full-size slide

  54. ϑϩʔ
    w Ϣʔβʔ͕4FBSDIΛ࣮ߦ
    &OUFSΩʔ

    w ͦΕʹԠͯ͡(&5
    3FRVFTUΛඈ͹͢ ᶄ

    View full-size slide

  55. 7JFX$POUSPMMFSͷ࡞੒ɾඥ෇͚

    View full-size slide

  56. 7JFX$POUSPMMFS
    w ViewΛ؅ཧ͢ΔΫϥε
    w ࠓ·Ͱ͸ViewControllerͷදࣔ͢ΔView
    Λઃఆ͍ͯͨ͠

    View full-size slide

  57. .BJO7JFX$POUSPMMFS࡞੒
    w ੍ޚͷͨΊʹMainViewControllerΛ࡞੒͢Δ

    View full-size slide

  58. .BJO7JFX$POUSPMMFS࡞੒
    w ੍ޚͷͨΊʹMainViewControllerΛ࡞੒͢Δ

    View full-size slide

  59. 7JFX$POUSPMMFSͷؔ࿈෇͚
    w $VTUPN$MBTTʹઃఆ

    View full-size slide

  60. 7JFXͷඥ෇͚
    w "TTJTUBOU&EJUPSӈʹMainViewController

    View full-size slide

  61. 7JFXͷඥ෇͚
    w 7JFXΛબ୒$USMΛԡ͠ͳ͕Βυϥοά

    View full-size slide

  62. 7JFXͷඥ෇͚
    w ૠೖ͍ͨ͠৔ॴʹΧʔιϧΛ߹ΘͤΔ

    View full-size slide

  63. 7JFXͷඥ෇͚
    w ໊લΛೖྗ

    View full-size slide

  64. 7JFXͷඥ෇͚
    w ඥ෇͚׬ྃʂʂʂ

    View full-size slide

  65. https://gyazo.com/10b37a14a684630c5d176d95ca529180

    View full-size slide

  66. Ωʔϫʔυऔಘ

    View full-size slide

  67. Ωʔϫʔυऔಘ
    • Ϣʔβʔ͕ݕࡧΛ࣮ߦ͢Δ
    → func searchBarSearchButtonClicked

    (searchBar: UISearchBar)

    ͕ݺ͹ΕΔ
    → searchBar ͔ΒςΩετΛऔಘ͢Δ

    View full-size slide

  68. import UIKit
    class MainViewController: UIViewController, UISearchBarDelegate {
    @IBOutlet weak var searchBar: UISearchBar!
    @IBOutlet weak var tableView: UITableView!
    override func viewDidLoad() {
    super.viewDidLoad()
    searchBar.delegate = self
    }
    }
    6*4FBSDI#BS%FMFHBUF
    • UISearchBarDelegate Λ࣮૷

    (SearchBar ؔ܎ͷϝιουΛ࣮ߦͰ͖ΔΑ͏ʹ͢Δ)

    View full-size slide

  69. import UIKit
    class MainViewController: UIViewController, UISearchBarDelegate {
    @IBOutlet weak var searchBar: UISearchBar!
    @IBOutlet weak var tableView: UITableView!
    override func viewDidLoad() {
    super.viewDidLoad()
    searchBar.delegate = self
    }
    }
    • searchBar ؔ܎ͷϝιουͷݺͼઌΛ self(ࣗ෼) ʹ
    6*4FBSDI#BS%FMFHBUF

    View full-size slide

  70. ϘλϯͷΫϦοΫ
    import UIKit
    class MainViewController: UIViewController, UISearchBarDelegate {

    override func viewDidLoad() {
    super.viewDidLoad()
    searchBar.delegate = self
    }
    func searchBarSearchButtonClicked(searchBar: UISearchBar) {
    let searchText = searchBar.text
    }
    }
    • searchBarSearchButtonClicked ͕ݺ͹ΕΔͷͰɺ

    searchBar ͷςΩετΛऔಘ

    View full-size slide

  71. "MFSUʹग़ͯ͠ΈΔ

    View full-size slide

  72. ᶄ"1*Ͱ+40/औಘ

    View full-size slide

  73. ωοτϫʔΫ
    w ࠓճ͸ϥΠϒϥϦΛ࢖Θͣʹੜ4XJGUͰ

    View full-size slide

  74. func searchBarSearchButtonClicked(searchBar: UISearchBar) {
    let searchText = searchBar.text
    let encodedText = searchText!.

    stringByAddingPercentEncodingWithAllowedCharacters(

    .URLQueryAllowedCharacterSet())!
    let url = "https://api.stackexchange.com/2.2/search?~~~~" +

    encodedText + "&site=stackoverflow&filter=!
    6JEajsykLFu3W"
    }
    63-Λ࡞੒
    w 63-
    Τϯίʔυ

    View full-size slide

  75. ϦΫΤετΛ౤͛Δ
    w J04Ͱ͸6*ͷߋ৽ʹ࢖͏εϨου͸Ұͭ
    w ͜ͷεϨουҎ֎Ͱ6*ؔ܎Λ࿔ΔͱΤϥʔ
    w ωοτϫʔΫͷϦΫΤετ͸ɺ

    ϝΠϯεϨουͰߦ͏΂͖Ͱ͸ͳ͍
    w ϦΫΤετʹ͕͔͔࣌ؒΔͱը໘͕ݻ·ΔͨΊ

    View full-size slide

  76. ϦΫΤετΛ౤͛Δ
    • NSURLSession
    w όοΫάϥ΢ϯυͰ௨৴ͯ͘͠ΕΔ
    w J04ΑΓ࢖͏͜ͱ͕Ͱ͖Δ

    View full-size slide

  77. NSURLSession.sharedSession().dataTaskWithURL(NSURL(string: url)!,

    completionHandler: {data, response, error in

    dispatch_async(dispatch_get_main_queue(), {
    let message = NSString(data: data!, …)! as String
    let alert = UIAlertController(…, message: message, …)
    alert.addAction(UIAlertAction(title: "OK", …))
    self.presentViewController(alert, …)
    })
    }).resume()
    • ϥϜμࣜͷ಺෦͸ϝΠϯεϨουͰ͸ͳ͍
    • ΞϥʔτΛग़͍͕ͨ͠མͪͯ͠·͏
    /463-4FTTJPOΛ࢖ͬͨϦΫΤετ

    View full-size slide

  78. NSURLSession.sharedSession().dataTaskWithURL(NSURL(string: url)!,

    completionHandler: {data, response, error in

    dispatch_async(dispatch_get_main_queue(), {
    let message = NSString(data: data!, …)! as String
    let alert = UIAlertController(…, message: message, …)
    alert.addAction(UIAlertAction(title: "OK", …))
    self.presentViewController(alert, …)
    })
    }).resume()
    • dispatch_get_main_queue() Λ࢖ͬͯ

    ϝΠϯεϨουͰ࣮ߦ
    • ͜͜Ͱ͸ΞϥʔτΛग़͍ͯ͠Δ
    /463-4FTTJPOΛ࢖ͬͨϦΫΤετ

    View full-size slide

  79. ᶅ+40/Λॲཧ

    View full-size slide

  80. +40/Λύʔε
    try! NSJSONSerialization.JSONObjectWithData(data!,

    options:NSJSONReadingOptions.MutableContainers)

    as! NSDictionary as! [String: AnyObject]
    • KTPOΛΦϒδΣΫτʹม׵

    View full-size slide

  81. +40/Λύʔε
    try! NSJSONSerialization.JSONObjectWithData(data!,

    options:NSJSONReadingOptions.MutableContainers)

    as! NSDictionary as! [String: AnyObject]
    • NSDictionary ʹม׵

    View full-size slide

  82. +40/Λύʔε
    try! NSJSONSerialization.JSONObjectWithData(data!,

    options:NSJSONReadingOptions.MutableContainers)

    as! NSDictionary as! [String: AnyObject]
    • Swift ʹ͓͚Δ࿈૝഑ྻʹม׵

    View full-size slide

  83. +40/Λύʔε
    try! NSJSONSerialization.JSONObjectWithData(data!,

    options:NSJSONReadingOptions.MutableContainers)

    as! NSDictionary as! [String: AnyObject]
    • ྫ֎Λແࢹ

    View full-size slide

  84. σʔλอ࣋Ϋϥε

    View full-size slide

  85. σʔλอ࣋Ϋϥε
    w ฦ͖ͬͯͨ+40/ΛಡΈࠐΈɺΫϥεʹ֨ೲ͢Δ
    w σʔλΛѻ͍΍͘͢͢ΔͨΊ

    View full-size slide

  86. σʔλอ࣋Ϋϥε
    • Question ΫϥεΛ࡞Δ
    • Question.swift Λ࡞੒

    View full-size slide

  87. import Foundation
    class Question {
    let isAnswered: Bool
    let ownerName: String
    let upVoteCount: Int
    let tags: [String]
    init(dict: [String: AnyObject]) {
    isAnswered = 

    dict["is_answered"] as! Bool
    upVoteCount = 

    dict["up_vote_count"] as! Int
    title = dict[“title"] as! String
    let owner = dict["owner"]

    as! [String: AnyObject]
    ownerName =
    owner["display_name"] as! String
    }
    }

    View full-size slide

  88. import Foundation
    class Question {
    let isAnswered: Bool
    let ownerName: String
    let upVoteCount: Int
    let tags: [String]
    init(dict: [String: AnyObject]) {
    isAnswered = 

    dict["is_answered"] as! Bool
    upVoteCount = 

    dict["up_vote_count"] as! Int
    title = dict[“title"] as! String
    let owner = dict["owner"]

    as! [String: AnyObject]
    ownerName =
    owner["display_name"] as! String
    }
    }

    View full-size slide

  89. import Foundation
    class Question {
    let isAnswered: Bool
    let ownerName: String
    let upVoteCount: Int
    let tags: [String]
    init(dict: [String: AnyObject]) {
    isAnswered = 

    dict["is_answered"] as! Bool
    upVoteCount = 

    dict["up_vote_count"] as! Int
    title = dict[“title"] as! String
    let owner = dict["owner"]

    as! [String: AnyObject]
    ownerName =
    owner["display_name"] as! String
    }
    }

    View full-size slide

  90. KTPOΛ2VFTUJPOʹม׵
    let dict = try! NSJSONSerialization.JSONObjectWithData(…)

    as! NSDictionary as! [String: AnyObject]
    let questions = (dict["items"] as! [[String: AnyObject]]).map{
    item -> Question in
    Question(dict: item)
    }

    View full-size slide

  91. RVFTUJPOT<>ͷUJUMFΛग़͢

    View full-size slide

  92. ᶆॲཧͨ݁͠ՌΛදࣔ

    View full-size slide

  93. 5BCMF7JFX
    ಉ͡7JFXΛ܁Γฦ͠Ϧετදࣔ͢Δ7JFX

    View full-size slide


  94. ී௨ʹ্͔ΒViewΛஔ͚͹͍͍͡ΌΜʁ

    View full-size slide

  95. w
    ⚠ཁૉ͕ݸҎ্ʹͳΔ͜ͱ΋
    w ྫ5XJUUFS
    w ͜ͷ࣌ݸಉ࣌ʹViewΛอ࣋ɾ؅ཧ͢Δʁ

    ˠ๲େͳϝϞϦɾॲཧΛফඅͯ͠͠·͏
    5BCMF7JFX

    View full-size slide

  96. w Ϧετදࣔ͸ಉ͡ߏ଄ͷView͕࿈ͳΔ
    w จࣈྻ౳ͷ಺༰͕ҧ͏͚ͩ
    ˠɹอ࣋ɾ؅ཧ͸ը໘ʹදࣔ͞ΕΔViewͷΈ
    ˠɹҰ౓࡞ͬͨView͸࠶ར༻͠ɺ

    දࣔ࣌ʹ಺༰Λมߋ͢Δ
    5BCMF7JFX

    View full-size slide

  97. w ͢΂ͯΛอ࣋ɾ؅ཧ
    ී௨ͷ7JFX
    View
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    ࣮ࡍͷදࣔྖҬ →

    View full-size slide

  98. w อ࣋ɾ؅ཧ͸ը໘্ͷΈ
    5BCMF7JFX
    TableView
    3
    4
    5
    6
    7
    8
    9
    10
    11
    ະੜ੒
    ࣮ࡍͷදࣔྖҬ →

    View full-size slide

  99. εΫϩʔϧ͞ΕΔͱʜ
    TableView
    3
    4
    5
    6
    7
    8
    9
    10
    11
    ະੜ੒
    View ͷ࠶ར༻
    දࣔྖҬ →

    View full-size slide

  100. εΫϩʔϧ͞ΕΔͱʜ
    View
    5
    6
    7
    8
    9
    10
    11
    3 ->12
    4 ->13
    ະੜ੒
    ← ৽͍͠஋Λઃఆ
    දࣔྖҬ →

    View full-size slide

  101. Ϧετදࣔ
    ߲໨ͷViewΛ࡞੒
    ˣ
    TableViewʹදࣔ

    View full-size slide

  102. ߲໨ͷ7JFX $FMM

    View full-size slide

  103. *EFOUJpFSΛઃఆ
    w TableView͕CellΛ۠ผ͢Δͷʹඞཁ

    View full-size slide

  104. w 2VFTUJPO5BCMF7JFX$FMMΛ࡞੒
    w $FMMΛಠࣗΫϥεʹͯ͠؅ཧ
    $VTUPN7JFXΫϥε
    import UIKit
    class QuestionTableViewCell: UITableViewCell {
    }

    View full-size slide

  105. $VTUPN7JFXʹඥ෇͚

    View full-size slide

  106. $FMM಺ͷ7JFXΛඥ෇͚

    View full-size slide

  107. $FMM಺ͷ7JFXΛඥ෇͚
    import UIKit
    class QuestionTableViewCell: UITableViewCell {
    @IBOutlet weak var countLabel: UILabel!
    @IBOutlet weak var checkImageView: UIImageView!
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var tagLabel: UILabel!
    @IBOutlet weak var bodyLabel: UILabel!
    @IBOutlet weak var dateLabel: UILabel!
    @IBOutlet weak var authorLabel: UILabel!
    }

    View full-size slide

  108. 2VFTUJPOઃఆͰ6*ߋ৽
    class QuestionTableViewCell: UITableViewCell {
    @IBOutlet weak var countLabel: UILabel!

    var question: Question? {
    didSet {
    bindQuestion(question!)
    }
    }
    private func bindQuestion(question: Question) {
    countLabel.text = String(question.upVoteCount)
    checkImageView.hidden = !question.isAnswered

    }
    }
    Setter

    View full-size slide

  109. 5BCMF7JFXʹදࣔ

    View full-size slide

  110. class MainViewController: UIViewController, UISearchBarDelegate, 

    UITableViewDelegate, UITableViewDataSource {
    @IBOutlet weak var searchBar: UISearchBar!
    @IBOutlet weak var questionTableView: UITableView!
    override func viewDidLoad() {
    super.viewDidLoad()
    searchBar.delegate = self
    questionTableView.dataSource = self
    questionTableView.delegate = self
    }
    }
    w Delegate Λ࣮૷
    5BCMF7JFX

    View full-size slide

  111. class MainViewController: UIViewController, UISearchBarDelegate, 

    UITableViewDelegate, UITableViewDataSource {
    @IBOutlet weak var searchBar: UISearchBar!
    @IBOutlet weak var questionTableView: UITableView!
    override func viewDidLoad() {
    super.viewDidLoad()
    searchBar.delegate = self
    questionTableView.dataSource = self
    questionTableView.delegate = self
    }
    }
    w UITableViewDelegateΫϦοΫ͞Εͨ࣌ͱ͔
    w UITableViewDataSourceσʔλ͔Β$FMMΛ࡞Δ࣌
    5BCMF7JFX

    View full-size slide

  112. • εΫϩʔϧͯ͠ Cell Λ࡞੒͢Δͱ͖ʹݺ͹ΕΔ
    • ৔ॴ͕Ҿ਺ͱͯ͠དྷΔ
    $FMMΛ࡞Δ
    func tableView(tableView: UITableView, cellForRowAtIndexPath 

    indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("questionCell",

    forIndexPath: indexPath) as! QuestionTableViewCell
    cell.question = self.questions[indexPath.row]
    return cell
    }

    View full-size slide

  113. • ઌ΄Ͳࢦఆͨ͠ Identifier ΛೖΕͯ

    ࠶ར༻Մೳͳ Cell ΛಘΔ
    • ͳ͚Ε͹ࣗಈͰ࡞ͬͯ͘ΕΔ
    $FMMΛ࡞Δ
    func tableView(tableView: UITableView, cellForRowAtIndexPath 

    indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("questionCell",

    forIndexPath: indexPath) as! QuestionTableViewCell
    cell.question = self.questions[indexPath.row]
    return cell
    }

    View full-size slide

  114. func tableView(tableView: UITableView, cellForRowAtIndexPath 

    indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("questionCell",

    forIndexPath: indexPath) as! QuestionTableViewCell
    cell.question = self.questions[indexPath.row]
    return cell
    }
    • ಘΒΕͨ Cell ͸ QuestionTableViewCell
    $FMMΛ࡞Δ

    View full-size slide

  115. func tableView(tableView: UITableView, cellForRowAtIndexPath 

    indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("questionCell",

    forIndexPath: indexPath) as! QuestionTableViewCell
    cell.question = self.questions[indexPath.row]
    return cell
    }
    • อ͍࣋ͯ͠Δ Question Λ cell ʹηοτ
    $FMMΛ࡞Δ

    View full-size slide

  116. w ηΫγϣϯͷ਺
    • Cell ͷ਺
    5BCMF7JFXͷઃఆ
    func numberOfSectionsInTableView(tableView: 

    UITableView) -> Int {
    return 1
    }
    func tableView(tableView: UITableView, 

    numberOfRowsInSection section: Int) -> Int {
    return self.questions.count
    }

    View full-size slide

  117. 5BCMF7JFXͷߋ৽
    let dict = try! NSJSONSerialization.JSONObjectWithData(…)

    as! NSDictionary as! [String: AnyObject]
    self.questions = (dict["items"] as! [[String: AnyObject]]).map{
    item -> Question in
    Question(dict: item)
    }
    self.questionTableView.reloadData()
    w tableViewʹσʔλͷߋ৽Λ఻͑Δ
    w ͜ΕͰCell͕࠶ಡΈࠐΈ͞ΕΔ

    View full-size slide

  118. ΫϦοΫͯ͠4BGBSJΛ։͘
    w tableViewͷCellΛΫϦοΫͨ࣌͠ʹݺ͹ΕΔ
    w CellΛ࡞Δؔ਺ͱ͸֎෦Ҿ਺໊͕ҧ͏
    func tableView(tableView: UITableView, didSelectRowAtIndexPath 

    indexPath: NSIndexPath) {
    let question = questions[indexPath.row]
    let url = NSURL(string: question.link)!
    UIApplication.sharedApplication().openURL(url)
    }

    View full-size slide

  119. w indexPath͔ΒQuestionΛऔಘ
    w question.linkΛSafariͰ։͘
    ΫϦοΫͯ͠4BGBSJΛ։͘
    func tableView(tableView: UITableView, didSelectRowAtIndexPath 

    indexPath: NSIndexPath) {
    let question = questions[indexPath.row]
    let url = NSURL(string: question.link)!
    UIApplication.sharedApplication().openURL(url)
    }

    View full-size slide

  120. ΞϓϦΛىಈ

    View full-size slide

  121. https://gyazo.com/d34d8f4caf0a48e1855c865cf1772748

    View full-size slide

  122. ײ૝
    w J04ྑ͍
    w 4XJGU͸࠷ߴ
    w όʔδϣϯݹ͍ͷʹରԠ͠ͳͯ͘ྑ͍
    w Ϗϧυૣ͍

    View full-size slide

  123. ײ૝
    w J04ͭΒ͍
    w 0CKFDUJWF$ͷίʔυ͕ಡΊͳ͍ͱݫ͍͠
    w ϥΠϒϥϦɾ4UBDL0WFS'MPX
    w υϥοάຖճ͢Δͷ໘౗
    w 9DPEF͕Α͘མͪΔʢىಈ͸ૣ͍ʣ

    View full-size slide

  124. ͋Γ͕ͱ͏͍͟͝·ͨ͠

    View full-size slide

  125. "OESPJEฤ͸ͪ͜Β

    View full-size slide