2017-03-06 72 views
0

我想把圖像(Appr。100+)放入UICollectionViewCell內的UIImageView中。由於這相當多,我試圖製作一個包含文件名稱並從中檢索名稱的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中拋出EXC_BAD_INSTRUCTION錯誤說gameIcon,也就是的UIImageView,是零,哪個不會的。我以爲我在UIImage(named:部分做了一些不正確的事,但事實並非如此。除此之外,我不知道這個錯誤來自哪裏。任何建議,將不勝感激。

  • 圖片資產正確放置在Assets.xcassets,我希望。
  • plist中的名稱匹配文件的名稱。
  • gameDataIcons數組確實有文件名的數組。
  • gameIcon已正確連接到UIImageView。

我對swift完全陌生,所以請原諒我,如果這個問題是基本問題。謝謝。

+0

仔細檢查你的'gameIcon'插座連接到你的故事板。 –

+0

在創建單元格之前,您將圖像分配給圖像視圖,這就是爲什麼它說imageview是零 –

回答

0

我建議先閱讀UICollectionView的文檔。看來你不明白它應該如何工作。

總之,您只能將一個UIImage分配給UIImageView。但在方法setGameDataIcon中,您嘗試將所有圖像分配到一個循環中的一個圖像視圖。

您可以將一個參數添加到setGameDataIcon,該參數是應分配給該特定單元格的圖像視圖的圖像的索引。 其次,您需要將gameCollectionViewCell().setGameDataIcon()viewDidLoad()移動到collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath),並使用indexPath.row作爲setGameDataIcon()的參數。

0

讓UIImageView爲零,表示您可能沒有在XiB中創建UIImageView的出口。
或者您必須如下指定屬性。

@IBOutlet weak var gameIcon: UIImageView = UIImageView() 

根據您的要求爲UIImageView分配幀!
在awakeFromNib中添加gameIcon UIImageView實例到你的單元格contentView。

override func awakeFromNib() { 
     super.awakeFromNib() 
     // Initialization code 
     self.contentView.addSubview(gameIcon) 
}