###前提・実現したいこと
UICollectionView で 写真をGrid表示したいができない。
###発生している問題・エラーメッセージ
let cellImage = UIImage(named: photos[indexPath.row])
の部分が
exc_bad_instruction(code=exc_1386_invop. subcode=0x0)
と表示入れる。
右下は
fatal error: Index out of range
エラーメッセージ
###該当のソースコード
import UIKit
class ViewController: UIViewController ,UICollectionViewDataSource, UICollectionViewDelegate {
//画像の名前を配列とする let photos = ["sample01", "sample02", "sample03", "sample04"] override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{ //Cellはストーリーボードで設定したセルのID let testCell:UICollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) //tag番号を使ってImageViewのインスタンス生成 let imageView = testCell.contentView.viewWithTag(1) as! UIImageView //画像配列の番号で指定された要素の名前の画像をUIImageとする let cellImage = UIImage(named: photos[indexPath.row]) //UIImageをUIImageViewのimageとして設定 imageView.image = cellImage //Tag番号を使ってLabelのインスタンスを生成 let label = testCell.contentView.viewWithTag(2) as! UILabel label.text = photos[indexPath.row] return testCell } //section 数の設定、今回は1つにセット func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { return 1 } func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { //要素数を入れる、要素以上の数字を入れると表示でエラーとなる return 10; } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. }
}
###補足情報(言語/FW/ツール等のバージョンなど) 参考 https://akira-watson.com/iphone/collectionview.html ・assets.xcassetsにアップした画像をグリッド表示することが目的です。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/08/07 13:05