設置UITableViewCell高度的問題

有很是多時候。UITableViewCell每行的高度是不固定的,需要動態設置。ide

UITableView有個代理方法,post

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return ((CZWeiboFrame*)[self.weiboFrames objectAtIndex:indexPath.row]).rowHeight;
    
}
有的會想在這裏先獲取cell。而後過去cell的高度再返回,但是這是不可行的,因爲上述方法會在

下述方法以前運行。atom

-(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設置數據時,現在數據模型中計算出高度。spa

如下是上述代碼中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>
}
相關文章
相關標籤/搜索