"Best Practice"에 대해 읽고/배우는 중에도 매우 익숙하지 만 간단한 코드를 리팩토링하려하지만 옵션을 중심으로 머리를 감싸고 간단한 UICollectionView
을 배치하는 데 어려움을 겪고 있습니다. UIViewController
.UIViewController 콜렉션 뷰 선택적인 언 래핑 크래시
나는 나가에서 "모범 사례"를 적용하려고 원하기 때문에 내가 이렇게이
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
}
}
처럼되고 싶은
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
}
}
작동하는, 무엇을 가지고 스위프트를 배우면서 시작하십시오. 즉,이 경우에 이해 한 것부터 시작하여 가능한 한 많은 것을 만들어야합니다.
private
weak
ify/"optionalize"뷰/아웃렛을 사용하여 c를 유지하지 마십시오. ycles.
문제
앱이 layout!.sectionInset
레벨에서 무을 풀기 때문에 강타 충돌.
나에게 기꺼이 좋은 영혼이 있습니까?
미리 감사드립니다.
왜 재산 약한 레이아웃입니까? 뷰 컨트롤러를 유지하고 있습니까? 선언문에서 초기화하지 않으시겠습니까? 그래서 당신은 unwrap을 강요 할 필요가 없습니다. : UICollectionViewFlowLayout = UICollectionViewFlowLayout = UICollectionViewFlowLayout() –