質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

2424閲覧

swift 2.2 非同期通信が終わってからCollectionViewを表示するにはどうしたらいいのでしょうか

chrokurojp

総合スコア26

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2016/10/06 06:31

編集2016/10/06 06:33

swift 2.2 非同期通信が終わってからCollectionViewを表示するにはどうしたらいいのでしょうか

ストーリーボード利用しています。

もともとのサンプルではXcode内に格納した画像を扱っているのですが

http://hajihaji-lemon.com/smartphone/swift/uicolloctionviewcontroller/

Alamofireで非同期通信して取得した画像URLを利用したいです。
Alamofireでの通信が終わる前にCollectionViewの表示が終わってしまいます。

ここで確認できました。 override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { // #warning Incomplete implementation, return the number of items print(self.articles.count) return 7 }

解決方法をご存知の方いらっしゃいましたら教えてください。

また、他のサンプルも試していますが

https://sites.google.com/a/gclue.jp/swift-docs/ni-yinki100-ios/uikit/uicollectionviewwo-shiu

ストーリーボードを利用しない場合
cellにURLから取得した画像を入れようとすると
[.image ]プロパティが無いとエラーになるのですが
この理由もわかっていません。

ViewController.swift

swift

1import UIKit 2 3class ViewController: UIViewController { 4 5 @IBOutlet weak var testNavBar: UINavigationBar! 6 @IBOutlet weak var testImageView: UIImageView! 7 8 var testTitle:String! 9 var testImage:UIImage! 10 11 override func viewDidLoad() { 12 super.viewDidLoad() 13 14 //タイトルと画像を設定する。 15 testNavBar.topItem!.title = testTitle 16 testImageView.image = testImage 17 18 } 19 20 //ボタン押下時の呼び出しメソッド 21 @IBAction func pushButton(sender: UIBarButtonItem) { 22 23 //モーダル表示されているビューコントローラーを解放する。 24 self.dismissViewControllerAnimated(true, completion:nil) 25 } 26 27}

TestCollectionViewController.swift

swift

1import UIKit 2 3import Alamofire 4import SwiftyJSON 5 6private let reuseIdentifier = "Cell" 7 8class TestCollectionViewController: UICollectionViewController { 9 10 var articles: [[String: String?]] = [] 11 12 override func viewDidLoad() { 13 super.viewDidLoad() 14 15 // Uncomment the following line to preserve selection between presentations 16 // self.clearsSelectionOnViewWillAppear = false 17 18 // Register cell classes 19 self.collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier) 20 21 // Do any additional setup after loading the view. 22 getArticles() 23 } 24 25 override func didReceiveMemoryWarning() { 26 super.didReceiveMemoryWarning() 27 // Dispose of any resources that can be recreated. 28 } 29 30 func getArticles() { 31 let url :String = "https://api.flickr.com/services/rest/" 32 33 let parameters :Dictionary = [ 34 "method" : "flickr.interestingness.getList", 35 "api_key" : "86997f23273f5a518b027e2c8c019b0f", 36 "per_page" : "300", 37 "format" : "json", 38 "nojsoncallback" : "1", 39 "extras" : "url_q,url_z", 40 ] 41 42 Alamofire.request(.GET, url, parameters: parameters) 43 .responseJSON { response in 44 45 guard let object = response.result.value else { 46 return 47 } 48 49 let json = JSON(object) 50 51 json["photos"]["photo"].forEach { (_, json) in 52 let article: [String: String?] = [ 53 "url_q": json["url_q"].string, 54 "url_z": json["url_z"].string 55 ] 56 self.articles.append(article) 57 } 58 print(self.articles) 59 } 60 } 61 62 /* 63 // MARK: - Navigation 64 65 // In a storyboard-based application, you will often want to do a little preparation before navigation 66 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 67 // Get the new view controller using [segue destinationViewController]. 68 // Pass the selected object to the new view controller. 69 } 70 */ 71 72 // MARK: UICollectionViewDataSource 73 74 override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { 75 // #warning Incomplete implementation, return the number of sections 76 return 1 77 } 78 79 80 override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 81 // #warning Incomplete implementation, return the number of items 82 print(self.articles.count) 83 return 7 84 } 85 86 override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 87// let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) 88 89 // Configure the cell 90 //セルを取得し、イメージビューに画像を設定して返す。 91 let cell = collectionView.dequeueReusableCellWithReuseIdentifier("TestCell", forIndexPath: indexPath) 92 let imageView = cell.contentView.viewWithTag(1) as! UIImageView 93 imageView.image = UIImage(named: "item" + String(indexPath.row) + ".png") 94 95 return cell 96 } 97 98 //セル選択時の呼び出しメソッド 99 override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 100 //セグエを実行する。 101 performSegueWithIdentifier("TestSegue", sender: nil) 102 103 } 104 105 //画面遷移実行前の呼び出しメソッド 106 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 107 108 //選択中のセルの画像を取得する。 109 let index = collectionView?.indexPathsForSelectedItems()?.first 110 let cell = collectionView?.cellForItemAtIndexPath(index!) 111 let imageView = cell!.viewWithTag(1) as! UIImageView 112 113 114 //遷移先のビューコントローラーを取得し、インスタンス変数に画像とテキストを設定する。 115 let controller:ViewController = (segue.destinationViewController as? ViewController)! 116 controller.testTitle = String(index!.row) 117 controller.testImage = imageView.image 118 } 119 120 121} 122

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

Swift

1func getArticles() { 2 3 〜 省略 〜 4 5 Alamofire.request(.GET, url, parameters: parameters) 6 .responseJSON { response in 7 8 〜 省略 〜 9 print(self.articles) 10 ******.reloadData() //******はCollectionViewの変数名 11 } 12}

Swift

1override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 2 return self.articles.count 3}

投稿2016/10/06 07:08

編集2016/10/06 07:10
Y_M

総合スコア265

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問