Dynamic size UICollectionView cell

六眼飞鱼酱① 提交于 2019-11-26 12:55:19

问题


\"enter

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

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

Current output: \"enter.


回答1:


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..




回答2:


Maybe this layout will fit your needs. Please take a look at this project: 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)




回答3:


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.

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.

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




回答4:


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



来源:https://stackoverflow.com/questions/23760275/dynamic-size-uicollectionview-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!