[英]My UICollectionViewCell item is nil?
My CollectionViewCell imageView is nil 我的CollectionViewCell imageView为零
My Cell File is here 我的细胞档案在这里
class MyTicketsCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var imgTicket: UIImageView!
}
My Controller File Code is here 我的控制器文件代码在这里
class MyTicketsCollectionViewController: UICollectionViewController {
var photos: [String] = ["ticket1","ticket2","ticket3","ticket4"]
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView?.register(MyTicketsCollectionViewCell.self, forCellWithReuseIdentifier: "MyTicketsCollectionViewCell")
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return photos.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyTicketsCollectionViewCell", for: indexPath) as! MyTicketsCollectionViewCell
cell.imgTicket.image = UIImage(named: photos[indexPath.row])
return cell
}
}
Error is "Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value" 错误为“线程1:致命错误:在展开可选值时意外发现nil”
When I debug it then
当我调试它时
cell.imgTicket is nil
How solve this issue? 如何解决这个问题?
您MyTicketsCollectionViewCell
ActiveTicketsCollectionViewCell
出队,但该单元格已在MyTicketsCollectionViewCell
下MyTicketsCollectionViewCell
。
Try changing your code to 尝试将代码更改为
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyTicketsCollectionViewCell", for: indexPath) as! MyTicketsCollectionViewCell
cell.imgTicket.image = UIImage(named: photos[indexPath.row])
return cell
}
There is 2 possible solutions here... 这里有2种可能的解决方案...
1. Might be ActiveTicketsCollectionViewCell have not the property imgTicket or you mention a wrong cell class here as u have mentioned a different cell class name MyTicketsCollectionViewCell so try to write this code. 1.可能是ActiveTicketsCollectionViewCell没有imgTicket属性,或者您在这里提到了错误的单元类,因为您提到了另一个单元类名称MyTicketsCollectionViewCell,因此请尝试编写此代码。
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyTicketsCollectionViewCell", for: indexPath) as! MyTicketsCollectionViewCell
cell.imgTicket.image = UIImage(named: photos[indexPath.row])
return cell
2. Might be you have missed to connect your property ( @IBOutlet weak var imgTicket: UIImageView!
) with the Outlet (xib or storyboard). 2.可能是您错过了将属性(
@IBOutlet weak var imgTicket: UIImageView!
)与插座(xib或情节提要)连接的可能性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.