+ (CGFloat)heightForRowWithModel:(PhotoInfo *)photoInfo
{
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ZZ" ofType:@"png"]];
CGFloat imageHeight = [self heightForImage:image];
CGFloat textHeight = [self heightForText:photoInfo.introduction];
return kPhotoCell_TitleLabel_Height + imageHeight + textHeight + 4 * kPhotoCell_MarginBetween;
}
+ (CGFloat)heightForImage:(UIImage *)image
{
CGSize size = image.size;
CGFloat scale = kPhotoCell_Width / size.width;
CGFloat imageHeight = size.height * scale;
return imageHeight;
}
+ (CGFloat)heightForText:(NSString *)text
{
NSDictionary *attrbute = @{NSFontAttributeName:[UIFont systemFontOfSize:kFontSize]};
return [text boundingRectWithSize:CGSizeMake(kPhotoCell_Width, 1000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attrbute context:nil].size.height;
}
创建一个UICollectionView 的方式:
UICollectionViewFlowLayout*layout=[[UICollectionViewFlowLayoutalloc]init];
[layoutsetScrollDirection:UICollectionViewScrollDirectionVertical];
layout.minimumInteritemSpacing=2;
layout.minimumLineSpacing=2;
_collec=[[UICollectionViewalloc]initWithFrame:CGRectMake(0.0,214.0,W,H)collectionViewLayout:layout];
_collec.delegate=self;
_collec.dataSource=self;
_collec.backgroundColor=[UIColorwhiteColor];
_collec.tag=100;
[self.viewaddSubview:_collec];
[_collecregisterNib:[UINibnibWithNibName:@"ZuoPinCell"bundle:nil]forCellWithReuseIdentifier:@"zuopinCell"];
[_collecregisterClass:[UICollectionReusableViewclass]forSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"header"];
实现头视图协议方式:
- (CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
returnCGSizeMake(320.0,50.0);
}
- (UICollectionReusableView*)collectionView:(UICollectionView*)collectionView viewForSupplementaryElementOfKind:(NSString*)kind atIndexPath:(NSIndexPath*)indexPath{
UICollectionReusableView*header;
if([kindisEqualToString:UICollectionElementKindSectionHeader]) {
header=[collectionViewdequeueReusableSupplementaryViewOfKind:kindwithReuseIdentifier:@"header"forIndexPath:indexPath];
[selfaddBtn];
[headeraddSubview:_btn];
}
returnheader;
}
- (void)addBtn{
UIButton*btn=[UIButtonbuttonWithType:UIButtonTypeCustom];
btn.frame=CGRectMake(0.0,0,320.0,50.0);
[btnsetTitle:@"上传+作品"forState:UIControlStateNormal];
btn.backgroundColor=[UIColorbrownColor];
[btnaddTarget:selfaction:@selector(handleWorkUp)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:btn];
_btn=btn;
}