对 Swift 还是很陌生,并且已经阅读/学习了“最佳实践”,我正在尝试重构一些简单的代码,但无法将我的头脑围绕在可选值上,并将一个简单的 UICollectionView
放在UIViewController
.
我有什么有用
class AddFriendsController: UIViewController {
fileprivate let cellId = "cellId"
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(handleCancel))
let layout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
layout.itemSize = CGSize(width: 111, height: 111)
let collectionViewTest = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
collectionViewTest.delegate = self
collectionViewTest.dataSource = self
collectionViewTest.register(UserFriendCell.self, forCellWithReuseIdentifier: cellId)
view.addSubview(collectionViewTest)
collectionViewTest.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
collectionViewTest.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
collectionViewTest.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
collectionViewTest.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true
}
@objc private func handleCancel() {
self.dismiss(animated: true, completion: nil)
}
}
extension AddFriendsController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 2
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 9
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! UserFriendCell
return cell
}
}
我想要的样子
class AddFriendsController: UIViewController {
fileprivate let cellId = "cellId"
private weak var collectionViewTest: UICollectionView?
private weak var layout: UICollectionViewFlowLayout?
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(handleCancel))
layout = UICollectionViewFlowLayout()
layout!.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
layout!.itemSize = CGSize(width: 111, height: 111)
collectionViewTest = UICollectionView(frame: self.view.frame, collectionViewLayout: layout!)
collectionViewTest!.delegate = self
collectionViewTest!.dataSource = self
collectionViewTest!.register(UserFriendCell.self, forCellWithReuseIdentifier: cellId)
view.addSubview(collectionViewTest!)
collectionViewTest!.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
collectionViewTest!.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
collectionViewTest!.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
collectionViewTest!.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true
}
@objc private func handleCancel() {
self.dismiss(animated: true, completion: nil)
}
}
extension AddFriendsController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 2
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 9
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! UserFriendCell
return cell
}
}
这样做是因为我想在学习 Swift 时从一开始就尝试应用“最佳实践”,即根据我在这种情况下的理解,尽可能多地制作 private
和 weak
确定/“可选化” View / socket 以避免保留周期。
问题
应用程序崩溃是因为在 layout!.sectionInset
级别解开一个 nil。
哪位好心人愿意开导我?
提前致谢。
最佳答案
您似乎在声明您的布局很薄弱。如果您将其更改为强,即只删除弱,那么您应该没问题。
对于您的 CollectionView 也是如此。
此外,作为样式问题,您可以声明您的布局和 collectionView 隐式展开,而不是每次都展开。
所以类似于 private var layout: UICollectionViewFlowLayout!
关于ios - UIViewController 中的 Collection View 可选展开崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42271782/