将映像附加到映像数组时IOS Swift线程中止错误
当我试图在IOS swift:Thread 1:SIGABRT中将图像附加到我的图像数组时,我遇到了这个错误 调用将映像附加到映像数组时IOS Swift线程中止错误,ios,swift,Ios,Swift,当我试图在IOS swift:Thread 1:SIGABRT中将图像附加到我的图像数组时,我遇到了这个错误 调用loadImages()函数时发生: import UIKit class ProtectedGallery: UIViewController, UICollectionViewDataSource { @IBOutlet weak var imageCollection: UICollectionView! var images = [UIImage]()
loadImages()
函数时发生:
import UIKit
class ProtectedGallery: UIViewController, UICollectionViewDataSource {
@IBOutlet weak var imageCollection: UICollectionView!
var images = [UIImage]()
override func viewDidLoad() {
super.viewDidLoad()
loadImages()
}
func loadImages()
{
images.append(UIImage(named: "image1")!)
self.imageCollection.reloadData()
}
func collectionView(_ imageCollection: UICollectionView, numberOfItemsInSection section: Int)-> Int{
return images.count
}
func collectionView(_ imageCollection: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = imageCollection.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! ImageCollectionViewCell
let image = images[indexPath.row]
cell.imageView.image = image;
return cell
}
}
你知道为什么会这样吗?我已经在这上面呆了一段时间。我刚刚注意到一些事情: 首先,从不为集合视图设置数据源(或委托)。这样做:
class ProtectedGallery: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
@IBOutlet weak var imageCollection: UICollectionView! {
didSet {
imageCollection.dataSource = self
imageCollection.delegate = self
}
}
}
此外,数据源方法参数的命名不正确。更改为:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// put your code here
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// put your code here
}
请显示更多代码我添加了更多代码将image1添加到项目你是什么意思?
UIImage(名为:“image1”)代码>加载图像,将其添加到资源中?我将数据源和代理出口链接到其类文件。我没看到旧的。或者我看错了。我的意思是:@ibvar-imageCollection:UICollectionView!从interface builder拖动时创建的。如果您只做过一次,那么应该可以,但是如果您多次拖动并创建了多个出口,则可能会导致崩溃。只做过一次我想,我在界面生成器上只右键单击了它,没有看到多个出口。好的,那么这不是问题所在。但当你右键单击时,至少会看到“imageCollection”,对吗?是的,我看到了,并且我将数据源和代理出口链接到了正确的“场景”。