首先我没有使用故事板这是完全以程序方式完成的,我无法让Cell出现在屏幕上,我相信这是因为函数collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath)
没有被调用。什么可能导致这种情况?
import UIKit
class ChatLOGVC: UICollectionViewController {
private let cellId = "Cell"
override func viewDidLoad() {
super.viewDidLoad()
collectionView?.backgroundColor = UIColor.red
collectionView?.register(FriendCell.self, forCellWithReuseIdentifier: cellId)
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
print("cell for item at index path called")
return collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize.init(width: view.frame.width, height: 80)
}
}
class FriendCell: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = UIColor.blue
}
required init?(coder aDecoder: NSCoder) {
fatalError()
}
}
答案 0 :(得分:0)
您必须使用以下布局展示您的收藏VC:
let layout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
let to_chat_controller = ChatLOGVC(collectionViewLayout: layout)