ios CollectionView sizeForItemAtIndexPath 从未调用过
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39107482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CollectionView sizeForItemAtIndexPath never called
提问by john doe
This is driving me crazy! I have a UICollectionViewController as shown below:
这真让我抓狂!我有一个 UICollectionViewController 如下所示:
class PhrasesCompactCollectionViewController: UICollectionViewController
The numberOfSections and cellForItemAt are being called but the sizeForItemAtIndexPath is never called. I am using the same exact code somewhere else and it fires correctly. I am using Xcode 8 Beta 6.
正在调用 numberOfSections 和 cellForItemAt,但从未调用过 sizeForItemAtIndexPath。我在其他地方使用完全相同的代码并且它正确触发。我正在使用 Xcode 8 Beta 6。
func collectionView(collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSize(width: 120, height:120)
}
回答by almas
You need to specify that you implement the protocol UICollectionViewDelegateFlowLayout
in your class declaration.
您需要UICollectionViewDelegateFlowLayout
在类声明中指定实现协议。
class PhrasesCompactCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout
class PhrasesCompactCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout
回答by Shiv Kumar
In Swift 3+Use this method:
在Swift 3+ 中使用此方法:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width:(collectionView.frame.height-90)/2, height: 100)
}
Make sure that your class complies both Delegate
确保您的班级符合委托
UICollectionViewDelegate
UICollectionViewDelegateFlowLayout
UICollectionViewDelegate
UICollectionViewDelegateFlowLayout
回答by Zenman C
回答by Gaurav Rami
Swift 3
斯威夫特 3
To set Cell Size of UICollectionView, you need to create and customise object of UICollectionViewFlowLayout
. Customise the properties and set that layout object to the UICollectionView object.
要设置 UICollectionView 的 Cell Size,您需要创建和自定义UICollectionViewFlowLayout
. 自定义属性并将该布局对象设置为 UICollectionView 对象。
Change cellheight and cell cellWidth objects according to your requirement (the cell hight and width you want).
根据您的要求(您想要的单元格高度和宽度)更改 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()
}
Make Sure: if you added sizeForItemAt indexPath method then must REMOVE the method...
确保:如果您添加了 sizeForItemAt indexPath 方法,则必须删除该方法...
Remove Below Method
删除下面的方法
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
}
回答by Xys
Swift 5
斯威夫特 5
All these steps are required:
所有这些步骤都是必需的:
- Conform to UICollectionViewDelegateFlowLayout<== Absolutely needed !!
- and to
UICollectionViewDelegate, UICollectionViewDataSource
- Set these in viewDidLoad :
collectionView.delegate = self
andcollectionView.dataSource = self
- Set Estimate Size to Nonein the Interface Builder
- Make sure the following method has sizeForItemAtand not sizeForItemAtIndexPath
- 符合UICollectionViewDelegateFlowLayout<== 绝对需要!!
- 并
UICollectionViewDelegate, UICollectionViewDataSource
- 在 viewDidLoad 中设置这些:
collectionView.delegate = self
和collectionView.dataSource = self
- 在 Interface Builder 中将Estimate Size设置为 None
- 确保以下方法具有sizeForItemAt而不是sizeForItemAtIndexPath
as :
作为 :
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
return CGSize(width: 100, height:100)
}
回答by H S Progr
First class need confirm to UICollectionViewDelegateFlowLayout. Then you need to write following code in viewDidLoad():
第一类需要确认 UICollectionViewDelegateFlowLayout。然后你需要在 viewDidLoad() 中编写以下代码:
//To tell the UICollectionView to use your UIViewController's UICollectionViewDelegateFlowLayout methods
//告诉 UICollectionView 使用你的 UIViewController 的 UICollectionViewDelegateFlowLayout 方法
collectionView.delegate = self
// If you want to set your own collection view flow layout
// 如果要设置自己的集合视图流布局
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical //depending upon direction of collection view
self.collectionView?.setCollectionViewLayout(layout, animated: true)
By using this code UICollectionViewDelegateFlowLayout method
通过使用此代码 UICollectionViewDelegateFlowLayout 方法
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
will get called, you can set size in this method.
将被调用,您可以在此方法中设置大小。
回答by Manish
Three things you need to check if no answer works:
如果没有答案,您需要检查三件事:
Implement FlowLayout Delegate :
class MyController: UICollectionViewController, UICollectionViewDelegateFlowLayout
Use method for size of item for 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) }
Select
Estimate size = None
in Inspect element of UICollectionView.
实现 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的元素。
回答by Stephen Rains
I had the same problem and nothing would fix it. I had a yellow warning saying to make it private because it came close to another function defined by the Layout Delegate. What fixed it for me was:
我有同样的问题,没有什么可以解决它。我有一个黄色警告说将其设为私有,因为它与布局委托定义的另一个函数很接近。为我修复的是:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
Notice the difference from "sizeForItemAtIndexPath" to the correct "sizeForItemAt".
注意从“sizeForItemAtIndexPath”到正确的“sizeForItemAt”的区别。
I tore my hair out for days trying to figure this out and it finally worked. Below I will include all of my function to show you how I also "hid" a cell by making the height equal to 0.
我把头发扯了好几天试图解决这个问题,它终于奏效了。下面我将包括我的所有函数,向您展示我如何通过使高度等于 0 来“隐藏”一个单元格。
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let findIndex = labelArray.index(of: "\(labelArray[indexPath.row])")
let screenSize = UIScreen.main.bounds
let screenWidth = screenSize.width
if (findIndex! % 2 == 0){
// Even index, show cell
return CGSize(width: screenWidth, height: 246)
}
else {
return CGSize(width: screenWidth, height: 0)
}
}
回答by Charlie Seligman
I had this issue last night. Finally solved it at midnight when I realised 'didSelectItemAtIndex' method was not closed off with a closure bracket "}"
我昨晚遇到了这个问题。当我意识到 'didSelectItemAtIndex' 方法没有用闭包括号“}”关闭时,终于在午夜解决了它
I added a closure bracket at the very bottom of the class when asked by the compile error. So in effect all the methods below 'didSelectItemAtIndex' were inside that method.
当编译错误询问时,我在类的最底部添加了一个闭包括号。因此,实际上“didSelectItemAtIndex”下面的所有方法都在该方法中。
Posting here just in case anyone else wastes 4 hours of their evenings on this one :-(
在这里发帖以防万一其他人在这个晚上浪费了 4 个小时:-(
回答by Azam
Make sure you put this in viewDidLoad()
确保你把它放在 viewDidLoad()
collectionView.delegate = self