这真让我抓狂!我有一个UICollectionViewController,如下所示:
class PhrasesCompactCollectionViewController: UICollectionViewController
Run Code Online (Sandbox Code Playgroud)
正在调用numberOfSections和cellForItemAt,但从不调用sizeForItemAtIndexPath.我在其他地方使用相同的确切代码,它正确触发.我正在使用Xcode 8 Beta 6.
func collectionView(collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSize(width: 120, height:120)
}
Run Code Online (Sandbox Code Playgroud)
alm*_*mas 221
您需要指定UICollectionViewDelegateFlowLayout
在类声明中实现协议.
class PhrasesCompactCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout
Shi*_*mar 49
在Swift 3+中使用此方法:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width:(collectionView.frame.height-90)/2, height: 100)
}
Run Code Online (Sandbox Code Playgroud)
确保您的班级符合两个代表
UICollectionViewDelegate
UICollectionViewDelegateFlowLayout
Xys*_*Xys 24
斯威夫特 5
所有这些步骤都是必需的:
UICollectionViewDelegate, UICollectionViewDataSource
collectionView.delegate = self
和collectionView.dataSource = self
作为 :
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
return CGSize(width: 100, height:100)
}
Run Code Online (Sandbox Code Playgroud)
Gau*_*ami 14
斯威夫特3
要设置UICollectionView的Cell Size,您需要创建和自定义对象UICollectionViewFlowLayout
.自定义属性并将该布局对象设置为UICollectionView对象.
根据您的要求(您想要的单元格高度和宽度)更改cellheight和cell cellWidth对象.
override func viewDidLoad() {
super.viewDidLoad()
let cellWidth : CGFloat = myCollectionView.frame.size.width / 4.0
let cellheight : CGFloat = myCollectionView.frame.size.height - 2.0
let cellSize = CGSize(width: cellWidth , height:cellheight)
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical //.horizontal
layout.itemSize = cellSize
layout.sectionInset = UIEdgeInsets(top: 1, left: 1, bottom: 1, right: 1)
layout.minimumLineSpacing = 1.0
layout.minimumInteritemSpacing = 1.0
myCollectionView.setCollectionViewLayout(layout, animated: true)
myCollectionView.reloadData()
}
Run Code Online (Sandbox Code Playgroud)
确定:如果你添加了sizeForItemAt indexPath方法,那么必须删除方法...
删除下面的方法
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
}
Run Code Online (Sandbox Code Playgroud)
如果没有答案,您需要检查三件事:
实现 FlowLayout 委托: class MyController: UICollectionViewController, UICollectionViewDelegateFlowLayout
swift 3.0+ 项目大小的使用方法: func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: (collectionView.frame.width / 3) - 1, height: collectionView.frame.width)
}
选择Estimate size = None
在检查UICollectionView的元素。
第一类需要确认到UICollectionViewDelegateFlowLayout。然后,您需要在viewDidLoad()中编写以下代码:
//告诉UICollectionView使用UIViewController的UICollectionViewDelegateFlowLayout方法
collectionView.delegate = self
Run Code Online (Sandbox Code Playgroud)
//如果要设置自己的收藏视图流布局
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical //depending upon direction of collection view
self.collectionView?.setCollectionViewLayout(layout, animated: true)
Run Code Online (Sandbox Code Playgroud)
通过使用此代码,UICollectionViewDelegateFlowLayout方法
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
Run Code Online (Sandbox Code Playgroud)
将被调用,您可以在此方法中设置大小。
归档时间: |
|
查看次数: |
42025 次 |
最近记录: |