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に正しく接続されています。
私は完全に迅速であるので、この質問が基本的なものであれば私を許してください。ありがとう。
ストーリーボードとの 'gameIcon'コンセント接続を再度確認してください。 –
セルを作成する前に、画像ビューに画像が割り当てられているので、imageviewがnilである –