动态改变tableCell的高度

//动态设置cell的高度

+ (CGFloat)heightForRowWithModel:(PhotoInfo *)photoInfo
{
    //1.图片的高度
    //让图片等比例缩放
    //(1)获取图片
    UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ZZ" ofType:@"png"]];
    CGFloat imageHeight = [self heightForImage:image];
    //2.文本的高度
    CGFloat textHeight = [self heightForText:photoInfo.introduction];
    //3.返回cell 的总高度
    return kPhotoCell_TitleLabel_Height + imageHeight + textHeight + 4 * kPhotoCell_MarginBetween;
}
//单独计算图片的高度
+ (CGFloat)heightForImage:(UIImage *)image
{
    //(2)获取图片的大小
    CGSize size = image.size;
    //(3)求出缩放比例
    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];

//设置cell间距

layout.minimumInteritemSpacing=2;

//设置cell行距

layout.minimumLineSpacing=2;

//设置头视图的大小

//layout.headerReferenceSize=CGSizeMake(320.0, 150.0);

_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"];

//注册collection头视图

[_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视图

UICollectionReusableView*header;

if([kindisEqualToString:UICollectionElementKindSectionHeader]) {

header=[collectionViewdequeueReusableSupplementaryViewOfKind:kindwithReuseIdentifier:@"header"forIndexPath:indexPath];

//添加头视图内容

[selfaddBtn];

//头视图添加view

[headeraddSubview:_btn];

}

returnheader;

}

//collection头视图

- (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;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值