我想把圖像(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完全陌生,所以請原諒我,如果這個問題是基本問題。謝謝。
仔細檢查你的'gameIcon'插座連接到你的故事板。 –
在創建單元格之前,您將圖像分配給圖像視圖,這就是爲什麼它說imageview是零 –