ios - Swift 中的 UICollectionView 自定义单元格大小
问题描述
我想通过使用 UICollectionView 获得以下布局,实现这一目标的最佳方法是什么。
我已经尝试过这种方法,但没有得到想要的结果
func collectionView(_ collectionView: UICollectionView, layout
collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath:
IndexPath) -> CGSize {
let totalWidth = collectionView.bounds.size.width
let totalHeight = collectionView.bounds.size.height
let heightOfView = totalHeight / 3
let numberOfCellsPerRow = 2
let dimensions = CGFloat(Int(totalWidth) / numberOfCellsPerRow)
if (indexPath.item == 0) {
return CGSize(width: collectionView.bounds.size.width, height: heightOfView)
} else {
return CGSize(width: dimensions / 2, height: heightOfView)
}
}
解决方案
将 UICollectionviewdelegateFlowlayout 添加到您的类并使用 collectionView(_:layout:sizeForItemAt:) 方法...在这里您可以根据索引路径返回项目的大小
推荐阅读
- javascript - 根据键值的存在从数组中删除元素
- php - 我需要帮助提取奇怪的 json 数据格式 PHP
- python-3.x - Property-Setter 装饰器的问题:未设置“私有”属性
- ms-access - 为什么需要 adOpenKeySet 将 ADO 记录集绑定到 Access 对象?
- javascript - Javascript绘制矩形边框
- vaadin - Vaadin 12 中标签字段内的超链接
- macos - 为什么使用 contentFilters 会改变整个窗口的外观,我可以停止它吗?
- node.js - NodeJS 以批处理方式停止和启动
- c# - Json.Net 序列化为自定义转换器提供了错误的属性
- ios - NWConnection SSDP 发现未接收数据