gpt4 book ai didi

将图像附加到图像数组时,IOS Swift 线程中止错误

转载 作者:行者123 更新时间:2023-11-29 05:15:56 25 4
gpt4 key购买 nike

当我尝试将图像附加到 IOS swift 中的图像数组时,出现此错误:线程 1:信号 SIGABRT

当我调用 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
}
}

有什么想法为什么会出现这种情况吗?我已经被这个问题困扰了一段时间了。

最佳答案

我刚刚注意到一些事情:

首先,您从未为 Collection View 设置数据源(或委托(delegate))。这样做:

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
}

关于将图像附加到图像数组时,IOS Swift 线程中止错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59185520/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com