首页 > 代码库 > 设置UITableViewCell高度的问题

设置UITableViewCell高度的问题

有非常多时候。UITableViewCell每行的高度是不固定的,须要动态设置。

UITableView有个代理方法,

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return ((CZWeiboFrame*)[self.weiboFrames objectAtIndex:indexPath.row]).rowHeight;
    
}
有的会想在这里先获取cell。然后过去cell的高度再返回,可是这是不可行的,由于上述方法会在

下述方法之前运行。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    CZWeiboFrame *model = self.weiboFrames[indexPath.row] ;
   
    static NSString *identifer = @"weibo";
    CZWeiboCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
    if(cell == nil){
        cell  = [[CZWeiboCell alloc ] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
        
    }
    cell.weiboFrame = model;
    
    return cell;
}

也就是说在你获取cell之前必须先设置cell的高度。

那么该怎么做呢。那就得在你给cell设置数据时,如今数据模型中计算出高度。

以下是上述代码中CZWeiboViewCell模型的结构

@interface CZWeiboFrame : NSObject

@property   (nonatomic,strong) CZWeibo *weibo;
@property   (nonatomic,assign,readonly) CGRect iconFrame;
@property   (nonatomic,assign,readonly) CGRect textFrame;
@property   (nonatomic,assign,readonly) CGRect nameFrame;
@property   (nonatomic,assign,readonly) CGRect pictureFrame;
@property   (nonatomic,assign,readonly) CGRect vipFrame;
@property (nonatomic,assign,readonly) CGFloat rowHeight;


@end
当中weibo是每行要显示的数据。row height是行高,其余是cell中每一个控件的frame,在weibo的set方法中计算frame

-(void)setWeibo:(CZWeibo *)weibo{
    _weibo = weibo;
    
    CGFloat margin = 10;
    CGFloat iconW = 35;
    CGFloat iconH = 35;
    CGFloat iconX = margin;
    CGFloat iconY = margin;
    _iconFrame = CGRectMake(iconX, iconY, iconW, iconH);
    
    CGFloat nameX = CGRectGetMaxX(_iconFrame)+margin;
    //依据Labele中文字的内容动态计算大小
    //头文件NSAttributString
    NSDictionary *attr = @{NSFontAttributeName:nameFont};
    CGSize nameSize = [self sizeWithText:_weibo.name size:CGSizeMake(MAXFLOAT, MAXFLOAT) font:nameFont];
    CGFloat nameW = nameSize.width;
    CGFloat nameH = nameSize.height;
    CGFloat nameY = iconY + (iconH - nameH)/2;
    
   _nameFrame  = CGRectMake(nameX, nameY, nameW, nameH);
    
    CGFloat vipW = 10;
    CGFloat vipH = 10;
    CGFloat vipX = CGRectGetMaxX(_nameFrame)+margin;
    CGFloat vipY = iconY +(iconH - vipH)/2;
    _vipFrame = CGRectMake(vipX, vipY, vipW, vipH);
    
    CGFloat textX = iconX;
    CGFloat textY = CGRectGetMaxY(_iconFrame)+margin;
    CGSize s = CGSizeMake([[UIScreen mainScreen] bounds].size.width - margin*2, MAXFLOAT);
    CGSize textSize = [self sizeWithText:weibo.text size:s font:textFont];
    
    _textFrame = CGRectMake(textX, textY, textSize.width, textSize.height);
    
    
    CGFloat picW = 200;
    CGFloat picH = 200;
    CGFloat picX = iconX;
    CGFloat picY = CGRectGetMaxY(_textFrame)+margin;
    _pictureFrame = CGRectMake(picX, picY, picW, picH)  ;
    
    _rowHeight = 0;
    if(self.weibo.picture){
        _rowHeight = CGRectGetMaxY(_pictureFrame)+margin;
    }else{
        _rowHeight = CGRectGetMaxY(_textFrame)+margin;
    }
}
///获取字符串的size
-(CGSize) sizeWithText:(NSString *) text size:(CGSize) size font:(UIFont *)font{
    NSDictionary *dict = @{NSFontAttributeName:font };
    CGSize sz = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;
    return sz;
}


然后我们就能给每一行设置高度了


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return ((CZWeiboFrame*)[self.weiboFrames objectAtIndex:indexPath.row]).rowHeight;<span style="font-family: Arial, Helvetica, sans-serif;">   </span>
}




设置UITableViewCell高度的问题