IOS中自定义cell大小的两种方法

第一种:自定义cell,不用xib的,用已经封装好的类Category.m类


   第一步: //label的自定义高度,用的是(封装好的方法)————————————————————根据label中的内容自动确定label的高度

                        //这个方法是UILabel分类中的:@implementation UILabel (UILabelCategory),所以只能UILabel对象引用给方法


      [label autolayoutContent :string origin : CGPointMake ( 10 , 10 ) FontFloat : 15 contentWidth : KScreenWidth - 20 ];

    

    [self.contentView addSubview:label];

    

    第二步: //再根据label的位置决定了每个cell的高度

    self.yzHeight = label.yzBottom + 10;


   第三步:在代理方法(tableView:heightForRowAtIndexPath:中最终设置,调用代理中的

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    //这个代理才是决定cell高度的关键 (调用代理方法tableView:cellForRowAtIndexPath:返回cell的对象)  

    CommentTableViewCell *cell = (CommentTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];

    return cell.yzHeight;

}




第二种:自定义cell,带xib的

     第一步:含有UILabel控件的Lines属性改为0;

        第二步:对xib中的所有控件进行相对约束,并不是全都相对cell本身

        第三步:设置以下两句话即可

            tableView.estimatedRowHeight = 44; //预计高度

            //根据cell中的子视图们的高度,从而确定cell的高度

            tableView.rowHeight = UITableViewAutomaticDimension;//自适应cell高度

    



你可能感兴趣的:(IOS中自定义cell大小的两种方法)