ios 动态大小 UICollectionView 单元格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23760275/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:10:06  来源:igfitidea点击:

Dynamic size UICollectionView cell

iosobjective-cuicollectionviewuicollectionviewcelluicollectionviewlayout

提问by Akshit Zaveri

enter image description here

在此处输入图片说明

1) How can i achieve as shown in image with UICollectionView?

1)我怎样才能实现如图所示UICollectionView

2) I've tried -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPathmethod & passed different sizes but it leaves out spaces between cells. Can i empty those spaces? I want different height & width for every cell

2)我尝试过-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath方法并传递了不同的大小,但它在单元格之间留下了空格。我可以清空这些空间吗?我想要每个单元格的高度和宽度不同

Current output:enter image description here.

当前输出:在此处输入图片说明

回答by RobMillerJ25

Sorry this is super late... But maybe this will help people who haven't found an answer yet. If you have your images in an array, you can simply reference the image size and make the adjustments to the cell size from there:

对不起,这太晚了......但也许这会帮助那些还没有找到答案的人。如果您将图像放在数组中,则可以简单地引用图像大小并从那里调整单元格大小:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UIImage *image = [imageArray objectAtIndex:indexPath.row];
//You may want to create a divider to scale the size by the way..
    return CGSizeMake(image.size.width, image.size.height); 
}

Hope this helps..

希望这可以帮助..

回答by Alexander Tkachenko

Maybe this layout will fit your needs. Please take a look at this project: https://github.com/betzerra/MosaicLayout

也许这种布局会满足您的需求。请看一下这个项目:https: //github.com/betzerra/MosaicLayout

I'm not sure 100% it will totally satisfy your requirements, but in the worst case it may help you in writing your custom UICollecionViewFlowLayout (which is the only way to achieve your goal)

我不确定它是否 100% 会完全满足您的要求,但在最坏的情况下,它可能会帮助您编写自定义 UICollecionViewFlowLayout(这是实现目标的唯一方法)

回答by Alessandro Orrù

You can't achieve a result like the one in your question with the standard UICollectionViewFlowLayout, that in its base implementation (without subclassing) creates a grid layout.

您无法使用标准实现问题中的结果UICollectionViewFlowLayout,即在其基本实现(无子类化)中创建网格布局。

To obtain what you want, you should create your own UICollectionViewLayout(or maybe UICollectionViewFlowLayout) subclass, where you perform all the computations needed for placing every item in the right frame.

为了获得你想要的东西,你应该创建你自己的UICollectionViewLayout(或者可能是UICollectionViewFlowLayout)子类,在那里你执行将每个项目放置在正确框架中所需的所有计算。

Take a look here for a great tutorialand here for something similar to what you want.

在这里查看一个很棒的教程,在这里查看与您想要的类似的东西

回答by Ashish Khobragade

Instead of creating variable heights of images in a row fix same sizes of heights of images but can have different widths in a row.Create such combinations and display and you can reuse same cell for different height with one or more images of different widths in row. i have done the sameit gives the same feel as you are looking for like google images search

不是在一行中创建可变高度的图像,而是修复相同大小的图像高度,但可以在一行中具有不同的宽度。创建这样的组合和显示,您可以将同一单元格重复用于不同高度的同一行中的一个或多个不同宽度的图像. 我已经做了同样的事情它给你同样的感觉,就像你在寻找谷歌图片搜索一样