第一次寫博客,寫的不規(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