Pack CollectionViewCells在iOS上紧密组合在一起? [英] Pack CollectionViewCells tightly together on iOS?

查看:147
本文介绍了Pack CollectionViewCells在iOS上紧密组合在一起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你想将单元格整齐地打包在一起,即使另一个单元格中的其他内容更大,那么如何做呢?



如果你看在下面的图片中,第一个字符串是最大的,但如果我想将其他单元格整齐地紧密地包装在一起,无论大小如何,这是如何完成的?



无论从侧面还是向上和向下,无论大小如何,我都希望这样做。
我想要在这张图片中的箭头指向时布置单元格。



更新:第一行文本是两个单元格打包到。 / p>

解决方案

你必须使用 UICollectionViewFlowLayout 实现此目的



注意:以下信息来自 Raywenderlich博客



您可以继承 UICollectionViewLayout 创建自己的自定义布局,但Apple慷慨地为开发人员提供了一个名为 UICollectionViewLayout 的基本基于流的布局。它根据大小一个接一个地放置元素,非常像网格视图。您可以开箱即用这个布局类,或者将其子类化以获得一些有趣的行为和视觉效果。



你可以在 Raywenderlich博客



示例代码:

   - (CGSize)collectionView:(UICollectionView *)collectionView布局:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
NSString * searchTerm = self.searches [indexPath.section]; FlickrPhoto * photo =
self.searchResults [searchTerm] [indexPath.row];

CGSize retval = photo.thumbnail.size.width> 0? photo.thumbnail.size:CGSizeMake(100,100);
retval.height + = 35; retval.width + = 35;返回;
}


- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView布局:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
返回UIEdgeInsetsMake(50,20,50,20);
}

您可以实现比此更多的委托方法。示例如下所示

  collectionView:layout:sizeForItemAtIndexPath 
collectionView:layout:insetForSectionAtIndex:


If you want to to pack the cells neatly together even if some of the other content in another cell is bigger, how do one do that?

If you look at the picture below, the first string is the biggest, but if I want to pack the other cells as neatly and tightly together as the first one, regardless of the size, how is this done?

I want to do this regardless of the size both from the sides and up and down. I want the cells to be laid out as the arrows in this picture are pointing.

UPDATE: the first line of text is two cells packed togheter.

解决方案

You have to use UICollectionViewFlowLayout to achieve this

Note: Below information's are taken from Raywenderlich blog

You can subclass UICollectionViewLayout to create your own custom layouts , but Apple has graciously provided developers with a basic "flow-based" layout called UICollectionViewLayout. It lays elements out one after another based on their size, quite like a grid view. You can use this layout class out of the box, or subclass it to get some interesting behavior and visual effects.

You can see a good example in Raywenderlich blog

Example code:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSString *searchTerm = self.searches[indexPath.section]; FlickrPhoto *photo =
    self.searchResults[searchTerm][indexPath.row];

    CGSize retval = photo.thumbnail.size.width > 0 ? photo.thumbnail.size : CGSizeMake(100, 100);
    retval.height += 35; retval.width += 35; return retval;
}


- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(50, 20, 50, 20); 
}

There are more delegate methods you can implement than this. Examples are given below

collectionView:layout:sizeForItemAtIndexPath
collectionView:layout:insetForSectionAtIndex:

这篇关于Pack CollectionViewCells在iOS上紧密组合在一起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆