标准MVC下动态生成UICollectionViewCell的size

第一次写博客,写的不规范的地方还望各位指点。(没有用过MarkDown,等发出来之后再根据格式改吧)

本人算是iOS程序员新人一枚,前几天在写一个Demo,需要用到UICollectionView,并且动态生成cell的size,搜了一下发现大部分都是动态生成UITableView的cell高度的文章。我去论坛问,发现并没有人回答,而且有几个跟我一样的新人有同样的疑惑,在找到方法之后决定写下这篇文章。这篇文章是MVC架构下没有使用Masonry条件下动态生成cell的size。

跟UITableView非常相似,UICollectionView也需要其viewController实现几个协议:UICollectionViewDataSource;UICollectionViewDelegate;UICollectionViewDelegateFlowLayout。

 根据MVC的思想:V跟M之间是相互解耦,所以有些文章中提到的用M保存V的size,在V内部访问M用于保存或者读取其size我觉得不可取。在不使用Masonry的情况下,其基本思想是在获取M的内容后,根据M的属性的具体内容计算出所需的V的size,其中这一思想可以分为两种实现方式:

第一种方式是由V计算,然后在协议中返回V的size的回调方法中实例化一个V,然后给这个V附上相应M的值,之后得到这个V的size,然后返回。

第二种方式是直接由C计算,这种方式是假设C知道V的全部细节,比如其ContentView的subViews的全部细节,因此我个人感觉这种方式不是很好,毕竟C的内容有已经很臃肿了,而且这种方式下存在一些代码冗余的现象,下面将详细说明(理解不对的话还请前辈们多指点)。
屏幕快照 2016-05-25 下午5.02.54.png

屏幕快照 2016-05-25 下午5.03.11.png

可以看到最后的效果非常简单。cell里面有两个label,这两个label将根据其内容自适应size,接着cell根据label的size自适应自己的size。

首先是第一种实现方式:在V的内部实现size的计算。

首先自定义cell,继承自UICollectionViewcell


@interface LCHCollectionViewCell : UICollectionViewCell

@property (nonatomic, strong) UILabel *firstLabel;

@property (nonatomic, strong) UILabel *secondLabel;

@end

然后重写initWithFrame方法,将label添加到contentView中


- (id)initWithFrame:(CGRect)frame {

self = [super initWithFrame:frame];

if (self) {

_firstLabel = [[UILabel alloc] init];

_firstLabel.numberOfLines = 1;

_firstLabel.textAlignment = NSTextAlignmentCenter;

_firstLabel.backgroundColor = [UIColor redColor];

_firstLabel.font = [UIFont systemFontOfSize:25];

_secondLabel = [[UILabel alloc] init];

_secondLabel.numberOfLines = 1;

_secondLabel.textAlignment = NSTextAlignmentCenter;

_secondLabel.backgroundColor = [UIColor greenColor];

_secondLabel.font = [UIFont systemFontOfSize:25];

[self.contentView addSubview:_firstLabel];

[self.contentView addSubview:_secondLabel];

self.backgroundColor = [UIColor blackColor];

}

return self;

}

最后重写layoutSubviews,用于在其label赋值之后可以调整V的Frame


- (void)layoutSubviews {

[super layoutSubviews];

CGSize sizeForFirstLabel = [self.firstLabel.text boundingRectWithSize:CGSizeMake([[UIScreen mainScreen] bounds].size.width, __FLT_MAX__) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,NSFontAttributeName:[UIFont systemFontOfSize:25]} context:nil].size;

self.firstLabel.frame = CGRectMake(0, 0, sizeForFirstLabel.width, sizeForFirstLabel.height);

CGSize sizeForSecondLabel = [self.secondLabel.text boundingRectWithSize:CGSizeMake([[UIScreen mainScreen] bounds].size.width, __FLT_MAX__) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,NSFontAttributeName:[UIFont systemFontOfSize:25]} context:nil].size;

self.secondLabel.frame = CGRectMake(sizeForFirstLabel.width, 0, sizeForSecondLabel.width, sizeForSecondLabel.height);

CGRect frame = self.contentView.frame;

frame.size = CGSizeMake(sizeForFirstLabel.width + sizeForSecondLabel.width, MAX(sizeForFirstLabel.height, sizeForSecondLabel.height));

self.contentView.frame = frame;

CGRect cellFrame = self.frame;

cellFrame.size = self.contentView.frame.size;

self.frame = cellFrame;

}

在C中实现UICollectionViewDelegateFlowLayout协议中的下列方法


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;

这个方法用于返回当前cell的Size


LCHCollectionViewCell *cell = [[LCHCollectionViewCell alloc] init];

LCHModel *model = self.models[indexPath.row];

cell.firstLabel.text = model.firstString;

cell.secondLabel.text = model.secondString;

[cell layoutIfNeeded];

CGRect frame = cell.frame;

return frame.size;

通过调用


[cell layoutIfNeeded];

当前cell已经重新布局,并生成了新的frame,获取其size之后返回,就是当前model的内容所对应的需要的view的size。

Demo的地址在这:
view内自适应size的Demo

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 写在前面 iOS的的布局机制「auto layout」不是一个新概念,它早在iOS 6中就推出来了,当下距离iOS...
    西门淋雨阅读 5,455评论 2 4
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,223评论 4 61
  • 相亲?你们哪来的自信? 猴哥,我的长相可是我们土波国第一美男子呢! 就是就是,我可是第二美男子呢,仅次于我师哥! ...
    神自刀阅读 1,358评论 0 0
  • 窗外的狂风,应该配上你的狂舞。。。 置身于黑暗,渴望夜空属于你的那颗星 尽管冰冷中,只会被珍惜的温暖灼伤 语言的隔...
    殇红树林阅读 2,669评论 0 0
  • 我漫步在街头 你浮游在上空 变幻身姿 四处漂泊 似乎在告诉我 你累了倦了 想要有个地方 温情降落 我斜倚在窗边 你...
    牧羊ysr阅读 2,435评论 3 5