標(biāo)準(zhǔn)MVC下動態(tài)生成UICollectionViewCell的size

第一次寫博客,寫的不規(guī)范的地方還望各位指點。(沒有用過MarkDown,等發(fā)出來之后再根據(jù)格式改吧)

本人算是iOS程序員新人一枚,前幾天在寫一個Demo,需要用到UICollectionView,并且動態(tài)生成cell的size,搜了一下發(fā)現(xiàn)大部分都是動態(tài)生成UITableView的cell高度的文章。我去論壇問,發(fā)現(xiàn)并沒有人回答,而且有幾個跟我一樣的新人有同樣的疑惑,在找到方法之后決定寫下這篇文章。這篇文章是MVC架構(gòu)下沒有使用Masonry條件下動態(tài)生成cell的size。

跟UITableView非常相似,UICollectionView也需要其viewController實現(xiàn)幾個協(xié)議:UICollectionViewDataSource;UICollectionViewDelegate;UICollectionViewDelegateFlowLayout。

 根據(jù)MVC的思想:V跟M之間是相互解耦,所以有些文章中提到的用M保存V的size,在V內(nèi)部訪問M用于保存或者讀取其size我覺得不可取。在不使用Masonry的情況下,其基本思想是在獲取M的內(nèi)容后,根據(jù)M的屬性的具體內(nèi)容計算出所需的V的size,其中這一思想可以分為兩種實現(xiàn)方式:

第一種方式是由V計算,然后在協(xié)議中返回V的size的回調(diào)方法中實例化一個V,然后給這個V附上相應(yīng)M的值,之后得到這個V的size,然后返回。

第二種方式是直接由C計算,這種方式是假設(shè)C知道V的全部細(xì)節(jié),比如其ContentView的subViews的全部細(xì)節(jié),因此我個人感覺這種方式不是很好,畢竟C的內(nèi)容有已經(jīng)很臃腫了,而且這種方式下存在一些代碼冗余的現(xiàn)象,下面將詳細(xì)說明(理解不對的話還請前輩們多指點)。
屏幕快照 2016-05-25 下午5.02.54.png

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

可以看到最后的效果非常簡單。cell里面有兩個label,這兩個label將根據(jù)其內(nèi)容自適應(yīng)size,接著cell根據(jù)label的size自適應(yīng)自己的size。

首先是第一種實現(xiàn)方式:在V的內(nèi)部實現(xiàn)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賦值之后可以調(diào)整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中實現(xiàn)UICollectionViewDelegateFlowLayout協(xié)議中的下列方法


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

這個方法用于返回當(dāng)前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;

通過調(diào)用


[cell layoutIfNeeded];

當(dāng)前cell已經(jīng)重新布局,并生成了新的frame,獲取其size之后返回,就是當(dāng)前model的內(nèi)容所對應(yīng)的需要的view的size。

Demo的地址在這:
view內(nèi)自適應(yīng)size的Demo

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 寫在前面 iOS的的布局機制「auto layout」不是一個新概念,它早在iOS 6中就推出來了,當(dāng)下距離iOS...
    西門淋雨閱讀 1,557評論 2 4
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,200評論 4 61
  • 相親?你們哪來的自信? 猴哥,我的長相可是我們土波國第一美男子呢! 就是就是,我可是第二美男子呢,僅次于我?guī)煾纾?...
    神自刀閱讀 205評論 0 0
  • 窗外的狂風(fēng),應(yīng)該配上你的狂舞。。。 置身于黑暗,渴望夜空屬于你的那顆星 盡管冰冷中,只會被珍惜的溫暖灼傷 語言的隔...
    殤紅樹林閱讀 338評論 0 0
  • 我漫步在街頭 你浮游在上空 變幻身姿 四處漂泊 似乎在告訴我 你累了倦了 想要有個地方 溫情降落 我斜倚在窗邊 你...
    牧羊ysr閱讀 312評論 3 5