Swift還很新,閱讀了解「最佳實踐」,我試圖重構一些簡單的代碼,但無法繞過選項並將簡單的UICollectionView
放在一個簡單的代碼中UIViewController
。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
}
}
這樣做是因爲我想嘗試從應用「最佳實踐」開始學習斯威夫特,即從我所瞭解的這種情況下儘可能多的東西private
和weak
ify /「optionalize」views/outlets,以避免保留c ycles。
問題
應用崩潰,因爲爆炸在layout!.sectionInset
水平展開一個零。
任何善良的靈魂願意開導我嗎?
在此先感謝。
爲什麼你佈局屬性弱?它保留視圖控制器嗎?你爲什麼不從聲明中初始化它?所以你不需要強制unwrap:'private let layout:UICollectionViewFlowLayout = UICollectionViewFlowLayout()' –