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 Slide

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

    View Slide

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

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

    ,PUMJOFUDʜ

    View Slide

  4. ࿩͢͜ͱ

    View Slide

  5. J04ͷجຊ

    View Slide

  6. J04ΞϓϦ࣮૷

    View Slide

  7. ࡞੒͢ΔΞϓϦ

    View Slide

  8. 4UBDL0WFSqPXݕࡧ

    View Slide

  9. 4UBDLFS

    View Slide

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

    View Slide

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

    View Slide

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

    w IUUQTBQJTUBDLFYDIBOHFDPNTFBSDI
    PSEFSEFTDTPSUBDUJWJUZJOUJUMFTXJGUTJUF
    TUBDLPWFSqPXpMUFSGQO[U%1

    View Slide

  13. View Slide

  14. (JU)VC
    IUUQTHJUIVCDPN
    OPOZMFOF4UBDLFSJ04

    View Slide

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

    View Slide

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

    View Slide

  17. J04

    View Slide

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

    View Slide

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

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

    View Slide

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

    View Slide

  21. 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 Slide

  22. ηοτΞοϓ

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  27. ॳظঢ়ଶΛோΊΔ

    View Slide

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

    View Slide

  29. .BJOTUPSZCPBSE

    View Slide

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

    View Slide

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

    View Slide

  32. )FMMP8PSME

    View Slide

  33. 4UPSZ#PBSEΛ։͘

    View Slide

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

    View Slide

  35. )FMMP 8PSMEͱهೖ

    View Slide

  36. ΞϓϦΛىಈ

    View Slide

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

    View Slide

  38. ΞϓϦ࡞੒

    View Slide

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

    View Slide

  40. ᶃݕࡧϫʔυΛೖྗ

    View Slide

  41. ϨΠΞ΢τ࡞੒

    View Slide

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

    View Slide

  43. 4FBSDI#BSͷઃஔ

    View Slide

  44. 5BCMF7JFXͷઃஔ

    View Slide

  45. 5BCMF7JFXΛ޿͛Δ

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  50. 7JFX่յ

    View Slide

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

    View Slide

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

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  59. 7JFX੒ޭ

    View Slide

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

    ·ܾͩΊ͍ͯͳ͍

    View Slide

  61. 4FBSDIˠจࣈྻऔಘ

    View Slide

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

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  74. Ωʔϫʔυऔಘ

    View Slide

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

    (searchBar: UISearchBar)

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

    View Slide

  76. 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 Slide

  77. 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 Slide

  78. ϘλϯͷΫϦοΫ
    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 Slide

  79. "MFSUʹग़ͯ͠ΈΔ

    View Slide

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

    View Slide

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

    View Slide

  82. 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 Slide

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

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

    View Slide

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

    View Slide

  85. 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 Slide

  86. 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 Slide

  87. ᶅ+40/Λॲཧ

    View Slide

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

    options:NSJSONReadingOptions.MutableContainers)

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

    View Slide

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

    options:NSJSONReadingOptions.MutableContainers)

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

    View Slide

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

    options:NSJSONReadingOptions.MutableContainers)

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

    View Slide

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

    options:NSJSONReadingOptions.MutableContainers)

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

    View Slide

  92. σʔλอ࣋Ϋϥε

    View Slide

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

    View Slide

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

    View Slide

  95. 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 Slide

  96. 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 Slide

  97. 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 Slide

  98. 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 Slide

  99. RVFTUJPOT<>ͷUJUMFΛग़͢

    View Slide

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

    View Slide

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

    View Slide


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

    View Slide

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

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

    View Slide

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

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide


  109. View Slide

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

    View Slide

  111. ߲໨ͷ7JFX $FMM

    View Slide

  112. ࡞Γ·ͨ͠

    View Slide

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

    View Slide

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

    View Slide

  115. $VTUPN7JFXʹඥ෇͚

    View Slide

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

    View Slide

  117. $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 Slide

  118. 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 Slide

  119. 5BCMF7JFXʹදࣔ

    View Slide

  120. 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 Slide

  121. 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 Slide

  122. • εΫϩʔϧͯ͠ 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 Slide

  123. • ઌ΄Ͳࢦఆͨ͠ 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 Slide

  124. 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 Slide

  125. 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 Slide

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

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

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

    View Slide

  127. 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 Slide

  128. ΫϦοΫͯ͠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 Slide

  129. 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 Slide

  130. ΞϓϦΛىಈ

    View Slide

  131. https://gyazo.com/d34d8f4caf0a48e1855c865cf1772748

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  135. "OESPJEฤ͸ͪ͜Β

    View Slide