我收到的错误消息通常与缺少的IBOutlet相关,但是当我添加一个错误时,我会得到更多的错误。我在这方面找到了一些答案,但都没有奏效。我做错了什么?
错误消息:
由于
异常'NSInternalInconsistencyException‘终止应用程序,原因:-UICollectionViewController loadView实例化视图控制器,标识符"UIViewController-BYZ-38-t0r“,来自故事板"Main",但没有得到一个UICollectionView。
import UIKit
import Photos
private let reuseIdentifier = "Cell"
class ViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
var imageArray = [UIImage]()
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
grabPhotos()
}
func grabPhotos(){
let imgManager = PHImageManager.default()
let requestOptions = PHImageRequestOptions()
requestOptions.isSynchronous = true
requestOptions.deliveryMode = .highQualityFormat
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
if let fetchResult : PHFetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions){
if fetchResult.count > 0 {
for i in 0..<fetchResult.count{
imgManager.requestImage(for: fetchResult.object(at: i), targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: requestOptions, resultHandler: {
image, error in
self.imageArray.append(image!)
})
}
} else {
print("you do not have any photos")
self.collectionView?.reloadData()
}
}
}
// MARK: UICollectionViewDataSource
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return imageArray.count
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 0
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
let imageView = cell.viewWithTag(1) as! UIImageView
imageView.image = imageArray[indexPath.row]
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = collectionView.frame.width / 3 - 1
return CGSize(width: width, height: width)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 1.0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 1.0
}
}
发布于 2020-09-25 11:18:28
启动新项目时,Xcode将为您提供带有自定义类UIViewController
的默认ViewController
。
不能简单地将类更改为:
class ViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
相反,从童话板中删除默认控制器并添加一个新的UICollectionViewController
注意,自定义类显示为UICollectionViewController
(灰色,因为它是默认类)。现在可以将其更改为ViewController
(在Attributes检查器窗格上将其设置为Is Initial View Controller
)。
https://stackoverflow.com/questions/64070034
复制相似问题