ben*_*661 1 uicollectionview swift
背景为黑色,单元格为白色,尽管任何单元格都未显示,但是没人simulator?会知道为什么会这样吗?
import UIKit
import Foundation
class ChatLog: UICollectionViewController, UITextFieldDelegate, UICollectionViewDelegateFlowLayout {
let cellId = "cellId"
override func viewDidLoad() {
super.viewDidLoad()
collectionView?.backgroundColor = UIColor.black
collectionView?.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellId)
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
cell.backgroundColor = UIColor.white
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.height, height: 80)
}
}
Run Code Online (Sandbox Code Playgroud)
更新:
如下以编程方式初始化ChatLog视图控制器意味着未调用UICollectionView数据源方法,例如collectionView(_:cellForItemAt:),未调用方法。
let newViewController = ChatLog(collectionViewLayout: UICollectionViewLayout())
替换为这个:
let newViewController = ChatLog(collectionViewLayout: UICollectionViewFlowLayout())
-
声明为时UICollectionViewController,实际上并不需要在代码中,集合视图的delegate和dataSource属性中进行显式设置。
只需确保在中Main.storyboard,UICollectionViewController通过单击视图控制器,然后单击身份检查器,将您的类设置为ChatLog。另外,请确保您已经单击UICollectionViewCell并将其标识符设置为“ cellId”。
如果这是一个多视图控制器项目,请确保可以通过将其设置为初始视图控制器或从另一个视图控制器向该视图控制器提供segue /导航来导航到ChatLog视图控制器。
下面的图片概述了我的解决方案。
| 归档时间: |
|
| 查看次数: |
1724 次 |
| 最近记录: |