UITableView中在iOS8之前經(jīng)常會遇到需要計(jì)算UITableViewCell單元格的高度,UICollectionViewCell同樣會遇到這樣的情況认境,不過在iOS8之后寬高計(jì)算就非常簡單了,只需要簡單設(shè)置一下即可.
UITableView自適應(yīng)
簡單看一下效果:
UITableView通用設(shè)置:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.data count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
BookTableViewCell *cell=[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
[cell setupModel:self.data[indexPath.row]];
return cell;
}
初始化數(shù)據(jù):
self.tableView.estimatedRowHeight=120.0f;
self.tableView.rowHeight=UITableViewAutomaticDimension;
self.data=[NSMutableArray array];
[self.tableView registerClass:[BookTableViewCell class] forCellReuseIdentifier:CellIdentifier];
for (NSInteger i=0; i<30; i++) {
BookModel *model=[BookModel new];
model.imgName=@"Book";
model.bookName=@"UITableViewCell";
NSInteger mode=i%3;
if (mode==0) {
model.bookDescription=@"魏則西挟鸠,男叉信,二十一歲,生前就讀于西安電子科技大學(xué)艘希,計(jì)算機(jī)專業(yè)學(xué)生硼身,因身患滑膜肉瘤去世。魏則西曾經(jīng)的夢想是每天玩命學(xué)習(xí)覆享,每天取得巨大的進(jìn)步佳遂,大四之后去美國好好學(xué)學(xué)計(jì)算機(jī),那會是他人生最大的幸福撒顿。";
}else if (mode==1){
model.bookDescription=@"滑膜肉瘤是一種惡性腫瘤丑罪,目前尚無有效治療手段。魏則西是家中獨(dú)子凤壁,父母傾盡全力為他治病吩屹。輾轉(zhuǎn)北京、上海拧抖、天津煤搜、廣州各大腫瘤醫(yī)院,得到的都是壞消息唧席。魏則西在百度搜索疾病信息擦盾,第一條結(jié)果是某武警醫(yī)院(武警北京總隊(duì)第二醫(yī)院)的所謂「生物免疫療法」。魏則西一家人在北京見到了這家武警醫(yī)院的李姓主任淌哟,李主任言:這個(gè)技術(shù)不是他們的迹卢,是斯坦福研發(fā)出來的,他們是合作绞绒,有效率達(dá)到百分之八九十婶希。看著魏則西的各種報(bào)告單蓬衡,李主任對魏則西父母說:保二十年沒問題喻杈。正所謂典型的套路:「病很重,能治好狰晚,得花錢」筒饰。";
}else if(mode==2){
model.bookDescription=@"魏則西一家人求醫(yī)心切,但也會有所顧慮壁晒,這不會是騙人的吧瓷们?不過一看這的確是一家三甲醫(yī)院,魏則西還專門查了一下這個(gè)醫(yī)生,這個(gè)李主任還上過中央電視臺谬晕,不止一次碘裕,中央電視臺十套節(jié)目。會是假的嗎攒钳?你看帮孔,百度、三甲醫(yī)院不撑、中央臺文兢,還是斯坦福的技術(shù),這些應(yīng)該沒有問題了吧焕檬。";
}
[self.data addObject:model];
}
UITableViewCell設(shè)置預(yù)估高度:
self.tableView.estimatedRowHeight=120.0f;
self.tableView.rowHeight=UITableViewAutomaticDimension;
順便可以設(shè)置一下刪除模式:
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
return @"刪除";
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.data removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}else if (editingStyle==UITableViewCellEditingStyleInsert){
NSInteger insertRow=indexPath.row+1;
BookModel *model=[BookModel new];
model.imgName=@"Book";
model.bookName=@"UITableViewCell";
model.bookDescription=[NSString stringWithFormat:@"插入成功:%ld",insertRow];
[self.data insertObject:model atIndex:insertRow];
[tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForItem:insertRow inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
設(shè)置插入模式:
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleInsert;
}
UICollectionView自適應(yīng)
自適應(yīng)效果圖:
UICollectionView數(shù)據(jù)初始化:
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.estimatedItemSize = CGSizeMake(100, 100);
[self.collectionView registerClass:[BookCollectionViewCell class] forCellWithReuseIdentifier:CollectionViewIdentifier];
self.collectionView.backgroundColor = [UIColor redColor];
self.collectionView.collectionViewLayout = layout;
self.data = [NSMutableArray array];
[self.data addObject:@"北京"];
[self.data addObject:@"FlyElephant"];
[self.data addObject:@"編程"];
[self.data addObject:@"加班"];
[self.data addObject:@"Objective-C"];
[self.data addObject:@"iOS"];
[self.data addObject:@"UICollectioView自適應(yīng)"];
}
UICollectionView初始化:
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [self.data count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
BookCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CollectionViewIdentifier forIndexPath:indexPath];
cell.contentLabel.text = self.data[indexPath.row];
return cell;
}
自定義BookCollectionViewCell:
@implementation BookCollectionViewCell
-(instancetype)initWithFrame:(CGRect)frame{
self=[super initWithFrame:frame];
if (self) {
self=[[[NSBundle mainBundle] loadNibNamed:@"BookCollectionCell" owner:self options:nil] firstObject];
self.backgroundColor=[UIColor darkGrayColor];
self.clipsToBounds=YES;
}
return self;
}
-(void)layoutSubviews{
[super layoutSubviews];
self.clipsToBounds=YES;
self.layer.cornerRadius=self.frame.size.width/8;
}
@end
iOS8之后的UITableView和UICollectionView自適應(yīng)都比較簡單設(shè)置高度即可姆坚,有疑問歡迎討論~