为什么收集视图控制器无缘无故地崩溃
我仍然不明白为什么我将uitaiteView单元格入dccappscollectionViewController
,然后完成后,我会遇到奇怪的问题。
import UIKit
class DCCAppsCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
struct Object {
var headerTitle: String!
var seactionName: [String]!
var seactionObjection: [String]!
var profile: [String]!
}
var objectArray = [Object]()
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Register cell classes
self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
// Do any additional setup after loading the view.
self.title = "Deaf Can Code Apps"
objectArray = [
Object(headerTitle: "In App Purchase", seactionName: ["Anime Comic App"], seactionObjection: ["Core Jezlleland"], profile: ["Icon 1"]),
Object(headerTitle: "",seactionName: ["Stickers Packs"], seactionObjection: ["Core Jezlleland Sticker"], profile: ["Icon 2"]),
Object(headerTitle: "Free",seactionName: ["Calculator app"], seactionObjection: ["Sign Calculator"], profile: ["Icon 3"]),
Object(headerTitle: "",seactionName: ["Tic Tac Toe App"], seactionObjection: ["SignTac"], profile: ["Icon 4"]),
// copy and paste
]
let collectionLayout = UICollectionViewFlowLayout()
collectionView = UICollectionView(frame: .zero, collectionViewLayout: collectionLayout)
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
// MARK: UICollectionViewDataSource
override func numberOfSections(in collectionView: UICollectionView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 0
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of items
return objectArray[section].seactionObjection.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
// Configure the cell
let imgIcon = UIImageView()
imgIcon.frame = CGRect(x: 24, y: 4, width: 128, height: 128)
imgIcon.image = UIImage(named: objectArray[indexPath.section].profile[indexPath.row])!
let textTitle = UILabel()
textTitle.text = objectArray[indexPath.section].seactionObjection[indexPath.row]
textTitle.frame = CGRect(x: 8, y: 144, width: 161, height: 21)
textTitle.textColor = UIColor.label
let textType = UILabel()
textType.text = objectArray[indexPath.section].seactionObjection[indexPath.row]
textType.frame = CGRect(x: 8, y: 173, width: 161, height: 16)
textType.textColor = UIColor.systemGray
let buttonOpen = UIButton()
buttonOpen.setTitle("Open", for: UIControl.State.normal)
buttonOpen.isMultipleTouchEnabled = true
buttonOpen.frame = CGRect(x: 8, y: 197, width: 161, height: 31)
cell.addSubview(textTitle)
cell.addSubview(textType)
cell.addSubview(imgIcon)
cell.addSubview(buttonOpen)
cell.backgroundView?.backgroundColor = UIColor.secondarySystemBackground
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 177, height: 243)
}
}
控制台说:
***终止由于未知的例外“ nsinvalidargumentException”终止应用程序,原因是: 用非nil布局参数'
初始化
我真的很压力,在研究导致这些问题的原因时无法入睡两天。自2020年以来,我从未在Xcode和Collection View本身的新更新中发行过我的发行,但现在遇到了不同的问题。每个人都知道是什么原因引起了问题吗?
I still do not understand why I faced kept getting strange issues when tapped the UITableView cell into open DCCAppsCollectionViewController
and then completed crashed for no reason.
import UIKit
class DCCAppsCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
struct Object {
var headerTitle: String!
var seactionName: [String]!
var seactionObjection: [String]!
var profile: [String]!
}
var objectArray = [Object]()
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Register cell classes
self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
// Do any additional setup after loading the view.
self.title = "Deaf Can Code Apps"
objectArray = [
Object(headerTitle: "In App Purchase", seactionName: ["Anime Comic App"], seactionObjection: ["Core Jezlleland"], profile: ["Icon 1"]),
Object(headerTitle: "",seactionName: ["Stickers Packs"], seactionObjection: ["Core Jezlleland Sticker"], profile: ["Icon 2"]),
Object(headerTitle: "Free",seactionName: ["Calculator app"], seactionObjection: ["Sign Calculator"], profile: ["Icon 3"]),
Object(headerTitle: "",seactionName: ["Tic Tac Toe App"], seactionObjection: ["SignTac"], profile: ["Icon 4"]),
// copy and paste
]
let collectionLayout = UICollectionViewFlowLayout()
collectionView = UICollectionView(frame: .zero, collectionViewLayout: collectionLayout)
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
// MARK: UICollectionViewDataSource
override func numberOfSections(in collectionView: UICollectionView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 0
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of items
return objectArray[section].seactionObjection.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
// Configure the cell
let imgIcon = UIImageView()
imgIcon.frame = CGRect(x: 24, y: 4, width: 128, height: 128)
imgIcon.image = UIImage(named: objectArray[indexPath.section].profile[indexPath.row])!
let textTitle = UILabel()
textTitle.text = objectArray[indexPath.section].seactionObjection[indexPath.row]
textTitle.frame = CGRect(x: 8, y: 144, width: 161, height: 21)
textTitle.textColor = UIColor.label
let textType = UILabel()
textType.text = objectArray[indexPath.section].seactionObjection[indexPath.row]
textType.frame = CGRect(x: 8, y: 173, width: 161, height: 16)
textType.textColor = UIColor.systemGray
let buttonOpen = UIButton()
buttonOpen.setTitle("Open", for: UIControl.State.normal)
buttonOpen.isMultipleTouchEnabled = true
buttonOpen.frame = CGRect(x: 8, y: 197, width: 161, height: 31)
cell.addSubview(textTitle)
cell.addSubview(textType)
cell.addSubview(imgIcon)
cell.addSubview(buttonOpen)
cell.backgroundView?.backgroundColor = UIColor.secondarySystemBackground
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 177, height: 243)
}
}
Console says:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be
initialized with a non-nil layout parameter'
I was really stressed and can't sleep for two days while researching what caused these issues. I never have been issued before in the new update of Xcode and Collection View itself since 2020 are worked as smoothly but now it was made different issues. Do everyone know what caused the issues?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论