###前提・実現したいこと
SwiftのUICollectionViewにURLから取得してきた画像を表示させたいです。
###発生している問題・エラーメッセージ
以下のようにnilが代入されてしまいます。
###ソースコード
swift
1 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 2 3 let cell : collectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCell", forIndexPath: indexPath) as! collectionViewCell 4 var error: NSError? 5 6 var imagePath = collection_img[indexPath.row] as! String 7 var imagePathUrl = NSURL(string: imagePath) 8 var imageData = NSData(contentsOfURL: imagePathUrl!) 9 cell.image?.image = UIImage(data: imageData!) 10 return cell 11 }
collection_imgは以下の通りに入っております。
[ "http://xxxxx1.png", "http://xxxxx2.png", "http://xxxxx3.png", "http://xxxxx4.png" ]
###補足情報(言語/FW/ツール等のバージョンなど)
xcode6.4を使用しております。
以下で試しても同じnilが表示される結果でした。
swift
1 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 2 3 let cell : collectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCell", forIndexPath: indexPath) as! collectionViewCell 4 var error: NSError? 5 6 let imagePathUrl = NSURL(string: "http://k.yimg.jp/images/top/sp2/cmn/logo-ns-131205.png") 7 var error: NSError? 8 var imageData = NSData(contentsOfURL: imagePathUrl!) 9 cell.image?.image = UIImage(data: imageData!) 10 return cell 11 } 12
URLを書いて下さい。書けないなら、http://k.yimg.jp/images/top/sp2/cmn/logo-ns-131205.png で試した結果を教えて下さい。