2017-03-06 10 views
0

UICollectionViewCell内のUIImageViewにイメージ(appr。100+)を配置しようとしています。それはかなりの量であるので、私はファイルの名前を含むplistファイルを作り、ファイル名を取得しようとしました。UICollectionVIew内のUIImageViewが表示されない

itemCollectionViewController

// 
// itemCollectionViewController.swift 
// phdrink 
// 
// Created by Canor on 2017. 3. 3.. 
// Copyright © 2017년 Scarlett Systems. All rights reserved. 
// 

import UIKit 

let reuseIdentifier = "itemCell" 
let screenRect: CGRect = UIScreen.main.bounds 
let screenSizeWidth = screenRect.width 

var gameDataIcons : [String] = [] // gameDataIcons is Array 

class itemCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Uncomment the following line to preserve selection between presentations 
     // self.clearsSelectionOnViewWillAppear = false 

     // Register cell classes 
     self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier) 


     // Do any additional setup after loading the view. 
     initGameDataIcon() 
     gameCollectionViewCell().setGameDataIcon() 
     collectionView?.reloadData() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    /* 
    // MARK: - Navigation 

    // In a storyboard-based application, you will often want to do a little preparation before navigation 
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     // Get the new view controller using [segue destinationViewController]. 
     // Pass the selected object to the new view controller. 
    } 
    */ 

    // MARK: UICollectionViewDataSource 

    override func numberOfSections(in collectionView: UICollectionView) -> Int { 
     // #warning Incomplete implementation, return the number of sections 
     return 1 
    } 

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     // #warning Incomplete implementation, return the number of items 
     return 99 
    } 


    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) 

     // Configure the cell 
     return cell 
    } 

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 

     print(screenSizeWidth/3.0) 
     return CGSize(width: (screenSizeWidth - 80.0)/2.0 , height: (screenSizeWidth - 80.0)/2.0) 
    } 

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat 
    { 

     return 20; 
    } 

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { 

     return 5; 
    } 

    func initGameDataIcon(){ 
     let inputFile = Bundle.main.path(forResource: "gamedata", ofType: "plist") 
     let inputDataArray = NSArray(contentsOfFile: inputFile!) 
     for inputItem in inputDataArray as! [Dictionary<String,String>]{ 
      let gameDataIcon = inputItem["img_filename"] 
      gameDataIcons.append(gameDataIcon!) 
     } 
    } 
} 

gameDataIcon.swift

// 
// gameDataIcon.swift 
// phdrink 
// 
// Created by Canor on 2017. 3. 4.. 
// Copyright © 2017년 Scarlett Systems. All rights reserved. 
// 

import Foundation 

class GameDataIcon{ 

    var value_img_filename: String 

    init(dataDictionary:Dictionary<String,String>){ 
     value_img_filename = dataDictionary["img_filename"]! 
    } 
} 

gameCollectionViewCell

// 
// gameCollectionViewCell.swift 
// phdrink 
// 
// Created by Canor on 2017. 3. 4.. 
// Copyright © 2017년 Scarlett Systems. All rights reserved. 
// 

import UIKit 

class gameCollectionViewCell: UICollectionViewCell { 

    @IBOutlet weak var gameIcon: UIImageView! 

    func setGameDataIcon(){ 
     for singlegameicon in gameDataIcons { 
      print(singlegameicon) 
      gameIcon.image = UIImage(named: singlegameicon) 
     } 
    } 
} 

が、私はシミュレータからこのコードを実行すると、Xcodeは、それはUIImageViewで、gameIconを言っEXC_BAD_INSTRUCTIONエラーがスローされます何もないそうではありません。私はUIImage(named:の部分で間違ったことをしていると思ったが、そうではなかった。それ以外に、私はこのエラーがどこから来るのか分からない。任意の提案をいただければ幸いです。

  • イメージアセットはAssets.xcassetsに正しく設定されています。
  • plistの名前は、ファイルの名前と一致します。
  • gameDataIcons配列にはファイル名の配列があります。
  • gameIconは、UIImageViewに正しく接続されています。

私は完全に迅速であるので、この質問が基本的なものであれば私を許してください。ありがとう。

+0

ストーリーボードとの 'gameIcon'コンセント接続を再度確認してください。 –

+0

セルを作成する前に、画像ビューに画像が割り当てられているので、imageviewがnilである –

答えて

0

まず、UICollectionViewのドキュメントを読むことをお勧めします。どのように働くのか分からないようです。

要するに、1つのUIImageをUIImageViewに割り当てることができます。しかし、方法setGameDataIconでは、すべてのイメージをループ内の1つのイメージビューに割り当てようとします。

setGameDataIconにパラメータを追加できます。このパラメータは、その特定のセルの画像ビューに割り当てる必要がある画像のインデックスです。 setGameDataIcon()のパラメータとしてindexPath.rowを使用してをviewDidLoad()からcollectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath)に移動する必要があります。

0

UIImageViewをnilにすると、XiBのUIImageViewのコンセントが作成されていない可能性があります。
または、以下のようにプロパティを割り当てる必要があります。

@IBOutlet weak var gameIcon: UIImageView = UIImageView() 

あなたの要件に応じてフレームをUIImageViewに割り当ててください!
gameIcon UIImageViewインスタンスをセルcontentViewに追加します。awakeFromNib内にもあります。

override func awakeFromNib() { 
     super.awakeFromNib() 
     // Initialization code 
     self.contentView.addSubview(gameIcon) 
} 
関連する問題