ios - SWIF 错误消息:实例化视图控制器...但没有获得 UICollectionView。
问题描述
我收到一条错误消息,通常与缺少的 IBOutlet 相关,但是当我添加一个时,我会收到更多错误。我在 SO 上找到了一些答案,但没有一个有效。我究竟做错了什么?
错误信息:
由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“-[UICollectionViewController loadView] 从情节提要“Main”中实例化了具有标识符“UIViewController-BYZ-38-t0r”的视图控制器,但没有获得 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
}
}
解决方案
当你开始一个新项目时,Xcode 会为你提供一个默认UIViewController
的自定义类ViewController
:
您不能简单地将类更改为:
class ViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
相反,从 Storyboard 中删除默认控制器并添加一个新的UICollectionViewController
:
请注意,自定义类显示为UICollectionViewController
(灰色,因为它是默认类)。您现在可以将其更改为ViewController
(并在 Attributes Inspector 窗格中将其设置为Is Initial View Controller
)。
推荐阅读
- mfc - 如何将编辑控件框中的用户输入数字用于在按钮后面编码的某些控制程序?
- reactjs - 如何使用 jest 和酶来测试代码拆分(使用 react-loadable 在路由级别进行代码拆分)
- visual-studio-code - 使用 Visual Studio 代码在评论中忽略 pep8(E501)
- css - 绝对定位在 IE 中无法正常工作,但在 Chrome 和 Firefox 中可以正常工作
- ruby-on-rails - 数据成功保存到数据库后,ajax url没有响应
- angular - 带有输入文本字段的 Angular 4 货币管道
- angular - Angular 应用程序(7.2 版)在 Firefox 上无法在 localhost 上运行
- python - 如何评估 KNeighborsRegressor 的决策边界
- javascript - 打字稿如何仅将接口中的属性和方法复制到新对象?
- sql-server - T-SQL 获取视图列定义