Доброго времени суток всем!
Кратко ситуация:
есть два CollectionView. В них должны отображаться изображения + подпись, но в примере добавляется только изображение.
Проблема - в одном контроллере всё отображается отлично, в другом аналогичные действия приводят к ошибке fatal error: unexpectedly found nil while unwrapping an Optional value Что обозначает ошибка - я знаю. Но вот причину обнаружить не получается.
Попытки определить причину ошибки результата не дали, потому (как к крайнему средству) обращаюсь к вам: в чем причина ошибки? Что делать в будущем, чтобы избежать подобных ситуаций?
Ниже собственно исходники:
КОНТРОЛЛЕР, ГДЕ ВСЁ РАБОТАЕТ (использован UICollectionViewController)
import UIKit
private let reuseIdentifier = "Cell"
class AddArticleCollectionViewController: UICollectionViewController {
var images = [UIImage]()
func imagesSet() -> [UIImage] {
var img: UIImage?
var result = [UIImage]()
img = UIImage(named: "apple")!
for _ in 1 ... 10 {
result.append(img!)
}
return result
}
override func viewDidLoad() {
super.viewDidLoad()
images = imagesSet()
self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of items
return images.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! MyCollectionViewCell
for img in images {
cell.imageIcon.image = img
}
return cell
}
}
Класс ячейки CollectionViewCell с двумя аутлетами:
import UIKit
class MyCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var imageIcon: UIImageView!
@IBOutlet weak var Label: UILabel!
}
КОНТРОЛЛЕР, ГДЕ ВЫСКАКИВАЕТ ОШИБКА ВРЕМЕНИ ИСПОЛНЕНИЯ (CollectionView встроен в ViewController):
import UIKit
class MainViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
var images = [UIImage]()
func imagesSet() -> [UIImage] {
var img: UIImage?
var result = [UIImage]()
img = UIImage(named: "apple")!
for _ in 1 ... 10 {
result.append(img!)
}
return result
}
override func viewDidLoad() {
super.viewDidLoad()
images = imagesSet()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return images.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! MyCollectionViewCell
for img in images {
cell.imageIcon.image = img
}
return cell
}
}