ios - 以编程方式声明的 UICollectionView 不会改变 Swift 5 中单元格的大小
问题描述
我正在以UICollectionView
编程方式声明 a 及其相应的单元格。写出原版设置后,我发现我无法配置单元格的大小。无论我是否放置逻辑或尺寸,单元格(红色)似乎都是一些默认的正方形(和小)大小。屏幕截图在这篇文章的末尾。
供参考,代码如下:
setupView(){}
// collection view for screens
let screenHeight : CGFloat = CGFloat(height/6)
let screenWidth = screenHeight * ( width/height )
let offset_top_screens : CGFloat = height - rowHeight - screenHeight
let screenRow = UICollectionView(
frame: CGRect(x: 0, y: offset_top_screens, width: width, height: screenHeight)
, collectionViewLayout: UICollectionViewFlowLayout()
)
//screenRow.translatesAutoresizingMaskIntoConstraints = false
screenRow.backgroundColor = Color.grayTertiary
// set up collection view delegates
screenRow.dataSource = self
screenRow.alwaysBounceHorizontal = true
screenRow.register(ScreenItem.self, forCellWithReuseIdentifier: ScreenItem.identifier) // register w/ storyboard
self.view.addSubview(screenRow)
self.screenRow = screenRow
self.view.bringSubviewToFront(screenRow)
self.screenHeight = screenHeight
self.screenWidth = screenWidth
// end collection view screen
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return dataSource.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let row = indexPath.row
let user = self.dataSource[row]
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ScreenItem", for: indexPath) as! ScreenItem
/*
cell.uuid = user.uuid
cell.delegate = self
cell.mediaSource = ... what is the pipe here?
*/
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 45, height:40) // changing these values do not change anything
// return CGSize(width: 90, height: collectionView.bounds.height) <- this does nothing
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) //.zero
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
// somewhere in a different file
/*
@Use: display user's screen
*/
class ScreenItem: UICollectionViewCell {
static var identifier: String = "ScreenItem"
weak var textLabel: UILabel!
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = Color.red
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func prepareForReuse() {
super.prepareForReuse()
self.reset()
}
func reset() {
// @TOOD: reset things here
}
}
__ 更新 __ 父类定义
class CallController:
UIViewController
, UICollectionViewDataSource
, UICollectionViewDelegate
解决方案
您似乎没有设置代表。如果您使用的是,UICollectionViewFlowLayout
那么您UIViewController
应该实现该UICollectionViewDelegateFlowLayout
协议。
UICollectionViewDelegateFlowLayout
您的UIViewController
实例是否已实现此协议?你设置了delegate
属性?
推荐阅读
- kubernetes - 调度程序不尊重 topologySpreadConstraints
- c# - 我可以比较数组项吗?下面的代码和解释
- javascript - 如何在我的传单地图中添加范围标签
- snowflake-cloud-data-platform - SNOWSQL 相当于 DBMS_OUTPUT.PUT_LINE()?
- javascript - 如何更改此布尔表达式返回 undefined 而不是 false?
- amazon-athena - 我正在尝试使用查询在 AWS Athena 中创建一个表
- node.js - 使用 Chai-http 在 mocha 测试用例中找不到 Done
- ios - 您可以推送到 viewController 并成为该控制器导航的一部分吗?
- doctrine-orm - 如何将迁移路径传递给教义:迁移:迁移?
- python - 创建一个新列,每行包含按欧几里得距离最近的行记录的行号