常用的第三方复制粘贴ios实用开发技巧

iOS 自定义不等高的cell

2016-11-28  本文已影响178人  azhang_coder

自定义不等高的cell


1.给模型增加frame数据(纯代码)

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AZStatus *status=self.statuses[indexPath.row];
    
    return status.cellHeight;
}
- (CGFloat)cellHeight
{
    
    if (_cellHeight==0) {
        CGFloat margin=10;
        ...
        // 图片frame
        if (self.picture) {//有配图
            CGFloat picX=margin*3;
            CGFloat picY=CGRectGetMaxY(self.textFrame)+margin;
            CGFloat picWH=200;
            self.pictureFrame=CGRectMake(picX, picY, picWH, picWH);
            _cellHeight=CGRectGetMaxY(self.pictureFrame)+margin;
        }else{//无配图
            
            _cellHeight=CGRectGetMaxY(self.textFrame)+margin;
        }

    }
    
    return _cellHeight;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        ...
        
        //配图
        UIImageView *pictureView=[[UIImageView alloc]init];
        [self.contentView addSubview:pictureView];
        self.picture1View=pictureView;  
        
    }
    
#import <UIKit/UIKit.h>
@class AZStatus;
@interface AZStatusCell : UITableViewCell

/*模型数据*/
@property(nonatomic,strong)AZStatus *status;

@end

-(void)setStatus:(AZStatus *)status
{
    _status=status;
    // 设置数据
    ...
    // 配图
    if (status.picture) {
        self.picture1View.hidden=NO;
        self.picture1View.image=[UIImage imageNamed:status.picture];
    }else{
        self.picture1View.hidden=YES;
    }
    
    //设置frame
    self.iconView.frame=status.iconFrame;
    self.nameLabel.frame=status.nameFrame;
    self.text_Label.frame=status.textFrame;
    self.vipView.frame=status.vipFrame;
    self.picture1View.frame=status.pictureFrame;
     
}

2.storyboard动态cell实现(iOS8以后)

-(void)setStatus:(AZStatus *)status
{
    _status=status;
    
    // 设置数据
    ...    
    // 配图
    if (status.picture) {
        self.picture1View.hidden=NO;
        self.picture1View.image=[UIImage imageNamed:status.picture];
        self.pictureTop.constant=10;
        self.pictureHeight.constant=100;
        
    }else{
        self.picture1View.hidden=YES;
        self.pictureHeight.constant=0;
        self.pictureTop.constant=0;
    }
    

    
}
    // self-sizing(iOS8以后,两个方法一起使用)
    // 设置tableView的高度为根据设定的约束自动计算
    self.tableView.rowHeight=UITableViewAutomaticDimension;
   
    // 设置tableView的估算高度
    self.tableView.estimatedRowHeight=44;

3.如果要支持iOS8之前

- (void)awakeFromNib
{
    // 手动设置文字的最大宽度(目的是:让label知道自己文字的最大宽度,进而能够计算出自己的frame)
    self.text_label.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 20;
}
// 告诉tableView所有cell的估算高度(设置了估算高度,就可以减少tableView:heightForRowAtIndexPath:方法的调用次数)
self.tableView.estimatedRowHeight = 200;
AZStatusCell *cell;
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 创建一个临时的cell(cell的作用:根据模型数据布局所有的子控件,进而计算出cell的高度)
    if (!cell) {
        cell = [tableView dequeueReusableCellWithIdentifier:ID];
    }

    // 设置模型数据
    cell.status = self.statuses[indexPath.row];

    return cell.height;
}

- (CGFloat)height
{
    // 强制布局cell内部的所有子控件(label根据文字多少计算出自己最真实的尺寸)
    [self layoutIfNeeded];

    // 计算cell的高度
    if (self.status.picture) {
        return CGRectGetMaxY(self.pictureImageView.frame) + 10;
    } else {
        return CGRectGetMaxY(self.text_label.frame) + 10;
    }
}
上一篇 下一篇

猜你喜欢

热点阅读