collectionviewを下記のプログラミングを用いて実装してみました。
新しく作ったプロジェクトではうまく実装することができたのですが、現在作ってる途中のプロジェクトに使うと「Thread1:signal SIGABRT」のエラーが起き、強制終了します。
サイトを見て鵜呑みにしている知識なのですがcollectionviewを使うときはcollectionviewのdataSourceとcollectionviewが乗っかってるviewcontrollerを結びつけないといけないと書いてました。
実際に新しく作ったプロジェクトでは結びつけることで実装できたのですが、途中のプロジェクトではdatasourceとviewcontrollerを結びつけるとエラーが起き、結びつけていないとエラーは起きないもののうまく表示されません。
collectionviewの乗っかっているviewcontrollerの状況は新しいものと途中のものは一緒にしているはずです。
なにかアドバイスだけでもいただけるとうれしいです。
サイトURL:https://akira-watson.com/iphone/collectionview.html
import UIKit class ViewController: UIViewController ,UICollectionViewDataSource, UICollectionViewDelegate { // 画像の名前を配列とする let photos = ["nagi", "toko","saya","yumiko","yuyu","yuka","miki","mai","kurumi","katakuriko"] override func viewDidLoad() { super.viewDidLoad() } func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{ // dequeueReusableCellWithReuseIdentifier の働きは // 再利用できるセルがあればそれを使う // 再利用できるセルがなければ生成する // 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. } }
回答1件
あなたの回答
tips
プレビュー