码迷,mamicode.com
首页 > 其他好文 > 详细

UI_UItableView_AutoCell(自定义cell 高度)

时间:2015-07-14 22:43:30      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:uitableview   cell自定义高度   label   ui   xcode   

#pragma mark 赋值方法
-(void)setCellDataWithModel:(NewsModel *)sender
{
    self.titleLabel.text = sender.title;
    self.summaryLabel.text = sender.summary;

    
    CGFloat height = [NewsCell getHeightWithModel:sender];
    
    // 把计算出来的高度赋值给label
    CGRect labelFrame = self.summaryLabel.frame;
    labelFrame.size.height = height;
    self.summaryLabel.frame = labelFrame;
    
}

#pragma mark - 计算高度的方法
+ (CGFloat)getHeightWithModel:(NewsModel *)model
{
    // 计算高度 (summaryLabel)
    // 宽度,高度必须定死一个
    NSDictionary *fontDic = @{NSFontAttributeName:[UIFont systemFontOfSize:17.0f]};
    
    CGRect rectFrame = [model.summary boundingRectWithSize:(CGSizeMake([UIScreen mainScreen].bounds.size.width - 20, MAXFLOAT)) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:fontDic context:nil];
    return rectFrame.size.height;
}

#pragma mark - 计算 cell 高度
+ (CGFloat)heightWithModel:(NewsModel *)model
{
    // 当做标记
    opeModel = model;
    return [self getHeightWithModel:model] + 20 + 25;

}


// 封装
+ (instancetype)getCinemaCellWithTableView:(UITableView *)tableView
{
    static NSString *cell_id = @"CinemaCell";
    CinemaCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_id];
    
//    if (cell == nil) {
//        cell = [[CinemaCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:cell_id];
//    }
    
    return cell;
}

#pragma mark - cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row < 10) {
        CinemaCell *cell = [CinemaCell getCinemaCellWithTableView:tableView];
        
        // 设置cell 的值
        [cell setCellDateWithModal:self.dataArray[indexPath.row]];
        
        return cell;
    } else {
        static NSString *cell_id1 = @"UITableViewCell";
        CinemaCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_id1];
        
        cell.textLabel.text = @"llldsoie";
        
        return cell;
    }
    
}



#pragma mark - cell 高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [NewsCell heightWithModel:self.dataArray[indexPath.row]];
}


版权声明:本文为outlan原创文章,未经博主允许不得转载。

UI_UItableView_AutoCell(自定义cell 高度)

标签:uitableview   cell自定义高度   label   ui   xcode   

原文地址:http://blog.csdn.net/yadong_zhao/article/details/46883039

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!