UICollectionViewでセルごとに違ったレイアウトで表示しようとしております。
カスタムセルで.xibファイルを使おうとするとThread1でSIGABRTエラーがでます。
Main.storyboardでUICollectionViewを配置し、カスタムクラスを作成したカスタムクラスにしてます。
Main.storyboardでcollection view cellのreuse identifierは設定していません。コードで設定しています。
.xibのfile's ownerはカスタムセルクラスに設定しています。
xibで作成したUIImageView(mainImageという名前)とカスタムセルクラスは接続していますが、エラーでは
. . setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key mainImage.' .
と表示され接続ができてないようです。
どこかの設定が不十分なんでしょうか?
swift
1import UIKit 2 3class ViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout { 4 5 var screenHeight: CGFloat! 6 var screenWidth: CGFloat! 7 8 let screenNames = ["main01", "main01", "main02", "main03", "main09", "main02", "main03", "main09", "main02"] 9 10 override func viewDidLoad() { 11 super.viewDidLoad() 12 // Do any additional setup after loading the view, typically from a nib. 13 self.automaticallyAdjustsScrollViewInsets = false 14 // セルのNibを登録 15 self.collectionView!.register(UINib(nibName: "CollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "cell") 16 } 17 18 // 希望のセルサイズを返す 19 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 20 21 let screenSize: CGRect = UIScreen.main.bounds 22 screenWidth = screenSize.width 23 screenHeight = screenSize.height 24 let returnSize = CGSize(width: screenWidth, height: screenHeight) 25 26 return returnSize 27 } 28 29 //セル間のマージンを削除 30 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { 31 return 0.0 32 } 33 34 //データの個数を返す 35 override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int 36 { 37 return screenNames.count 38 } 39 40 //データを返す 41 override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 42 { 43 let cell: CollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath) as! CollectionViewCell 44 45 cell.mainImage.image = UIImage(named: screenNames[indexPath.row]) 46 47 return cell 48 } 49 50 override func didReceiveMemoryWarning() { 51 super.didReceiveMemoryWarning() 52 // Dispose of any resources that can be recreated. 53 } 54}
カスタムセルクラス
import UIKit class CollectionViewCell: UICollectionViewCell { @IBOutlet weak var mainImage: UIImageView! override func awakeFromNib() { super.awakeFromNib() // Initialization code } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/02/07 00:23
2017/02/07 03:43
退会済みユーザー
2017/02/07 08:47
2017/02/07 11:42
退会済みユーザー
2017/02/07 13:22