iOS UICollectionView 原型单元格大小属性被忽略
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14492186/
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
iOS UICollectionView prototype cell size property ignored
提问by minerat
I'm using a UICollectionView with two prototype cells. The prototype cells have different widths and contain different controls (image view and web view). I'm definitely returning the correct prototype cell for a given index (all the cells display the correct content), but the prototype cell size is ignored and the collection view's item size is used instead. It's not like I'm manually setting the size. What's the point of allowing the prototype cell to be sized in storyboard if the property is just ignored when it's actually displayed?
我正在使用带有两个原型单元格的 UICollectionView。原型单元格具有不同的宽度并包含不同的控件(图像视图和网络视图)。我肯定会为给定的索引返回正确的原型单元格(所有单元格都显示正确的内容),但原型单元格大小被忽略,而使用集合视图的项目大小。这不像我手动设置大小。如果属性在实际显示时被忽略,那么允许原型单元格在故事板中调整大小有什么意义?
回答by Alex the Ukrainian
The size of the cell in the storyboard editor is just to help you design the cell. Since each prototype cell can be a different size, UICollectionView doesn't know which cell's size to pick for all the cells. That's why you set the actual size you want to use for your cells separately. You can do it in the designer by selecting the collection view and setting its Cell Size Width and Height in the Size inspector, under the "Collection View Size" heading.
故事板编辑器中单元格的大小只是为了帮助您设计单元格。由于每个原型单元格可以有不同的大小,UICollectionView 不知道为所有单元格选择哪个单元格的大小。这就是为什么要单独设置要用于单元格的实际大小的原因。您可以在设计器中通过选择集合视图并在“集合视图大小”标题下的“大小”检查器中设置其单元格大小宽度和高度来完成此操作。
Or, you can override the following method and return a CGSize object that specifies the size you want to use for each cell. Using this method, you can actually have each cell be a different size:
或者,您可以覆盖以下方法并返回一个 CGSize 对象,该对象指定要用于每个单元格的大小。使用这种方法,您实际上可以让每个单元格具有不同的大小:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
Example:
例子:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(100, 100);
}
Your view controller needs to be a delegate of UICollectionViewDelegateFlowLayout in order for this method to be called. So don't forget to add that delegate declaration to your view controller's .h file, such as:
您的视图控制器需要是 UICollectionViewDelegateFlowLayout 的委托才能调用此方法。所以不要忘记将该委托声明添加到您的视图控制器的 .h 文件中,例如:
@interface MyViewController () <UICollectionViewDelegateFlowLayout>
Swift
迅速
extension MyViewController : UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 100, height: 100)
}
}
回答by Sam
If all the cells are the same size and you can set the itemSize
on the UICollectionViewFlowLayout
如果所有单元格大小相同,您可以itemSize
在UICollectionViewFlowLayout
Example:
例子:
((UICollectionViewFlowLayout *) self.collectionViewLayout).itemSize = CGSizeMake(100, 100);
回答by mxwt
Swift, working for latest iOS.
Swift,适用于最新的 iOS。
(collectionView.collectionViewLayout as! UICollectionViewFlowLayout).itemSize = cellSize
I don't have enough points to reply to Sam, but the key is that it's not UICollectionViewLayout but UICollectionViewFlowLayout.
我没有足够的分数来回复 Sam,但关键是它不是 UICollectionViewLayout 而是 UICollectionViewFlowLayout。
回答by Roshan
I had a Similar Issue, I set a breakpoint on the sizeForItemAt and it was being called for every cell I had on my collection, however, the size set in code was never being reflected on the device.
我有一个类似的问题,我在 sizeForItemAt 上设置了一个断点,并且我的集合中的每个单元格都会调用它,但是,代码中设置的大小从未反映在设备上。
While looking at other collections in my project I noticed the Collection View Flow Layout was slightly different in the collection with the issue.
在查看我的项目中的其他集合时,我注意到集合视图流布局在有问题的集合中略有不同。
By Selecting the Flow Layout in the Storyboard
通过在 Storyboard 中选择 Flow Layout
And then Setting the Cell Size& Estimate Sizein the Size Inspector
然后在大小检查器中设置单元格大小和估计大小
This helped me solve my issue.
这帮助我解决了我的问题。
However the size of the cell will not be dynamic with what is in your sizeForItemAt
但是,单元格的大小不会随 sizeForItemAt 中的内容而动态变化
回答by Muhammad Shariq Hasnain
Try to use UICollectionViewDelegateFlowLayout method. In Xcode 11 or later, you need to set Estimate Size to none from Storyboard.
尝试使用 UICollectionViewDelegateFlowLayout 方法。在 Xcode 11 或更高版本中,您需要将 Storyboard 中的 Estimate Size 设置为 none。
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout:
UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let padding: CGFloat = 170
let collectionViewSize = advertCollectionView.frame.size.width - padding
return CGSize(width: collectionViewSize/2, height: collectionViewSize/2)
}
回答by S E
In Swift you can call sizeForItemAtIndexPath delegate method to set the size for each cell.
在 Swift 中,您可以调用 sizeForItemAtIndexPath 委托方法来设置每个单元格的大小。
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
if indexPath.row == 0 {
// Set the size for cell no. 0
}else if indexPath.row == 1 {
// Set the size for cell no. 1
}
}
I hope that helps. Thanks!
我希望这有帮助。谢谢!
回答by Wanbok Choi
You can set size of UICollectionViewCell
in UICollectionView
properties.
您可以UICollectionViewCell
在UICollectionView
属性中设置大小。
In Storyboard, Select Collection View
at the Document Outline
, then edit your Cell Size
in Size Inspector
.
在 Storyboard 中,Collection View
在 处选择Document Outline
,然后Cell Size
在Size Inspector
.
And you should change Cell's size type to Default, if it is Custom.
并且您应该将单元格的大小类型更改为默认值,如果它是自定义的。
回答by theDuncs
If all your cells are the same size, you should simply set the UICollectionView
's Cell size properties to the same as the UICollectionViewCell
's size properties.
如果所有单元格的大小都相同,则只需将UICollectionView
的单元格大小属性设置为与UICollectionViewCell
的大小属性相同。
回答by Naman Vaishnav
Use its Default Delegate . . .
使用它的默认委托。. .
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGSize mElementSize;
mElementSize = CGSizeMake(SCREEN_WIDTH , SCREEN_HEIGHT *0.60);
return mElementSize;
}