iOS UITableView cell自適應內容高度
定義UITableView 並且遵守兩個協議
firstTableView = [[UITableViewalloc]initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight-64) style:UITableViewStylePlain];
firstTableView.separatorStyle = NO;
firstTableView.delegate = self;
firstTableView.dataSource = self;
[self.viewaddSubview:firstTableView
//返回每個cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
FirstFrame *firstFrame = [frameArr objectAtIndex:indexPath.row];
return firstFrame.heightTableViewCellRow;
}
//cell的內容
/*
FirstData *firstData = [FirstDataarrayWithDic:dic];//網路載入json解析為物件
FirstFrame *firstFrame = [[FirstFrame alloc]init];//計算cell高度的累
firstFrame.firstData = firstData;
[firstDataArr addObject:firstData];//資料類陣列
[frameArr addObject:firstFrame];//cell高度類陣列
*/- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
{
FirstTableViewCell *cell = [FirstTableViewCelltheCorpusWithTableView:tableView];//自定義cell
cell.firstFrame = [frameArr objectAtIndex:indexPath.row];
cell.firstData = [firstDataArr objectAtIndex:indexPath.row];
return cell;
}
#import "FirstTableViewCell.h"
#import "FirstFrame.h"
#import "FirstData.h"
@implementation FirstTableViewCell
+(instancetype)theCorpusWithTableView:(UITableView *)tableView
{
static NSString *cellID = @"cellid";
FirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell) {
cell = [[FirstTableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellID];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
return cell;
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
_lineView = [[UIView alloc]init];
[self.contentViewaddSubview:_lineView];
_timeBackView = [[UIViewalloc]initWithFrame:CGRectMake(12, 15, 120, 17)];
[self.contentViewaddSubview:_timeBackView];
_titleLabel = [[UILabel alloc]init];
[self.contentViewaddSubview:_titleLabel];
_faceIageView = [[UIImageViewalloc]init];
[self.contentViewaddSubview:_faceIageView];
}
return self;
}
/*。 重寫set方法
cell.firstFrame = [frameArr objectAtIndex:indexPath.row];
cell.firstData = [firstDataArr objectAtIndex:indexPath.row];
*/-(void)setFirstData:(FirstData *)firstData
{
_firstData = firstData;
_timeLabel.text = _firstData.createtime;
_titleLabel.text = _firstData.title;
}
-(void)setFirstFrame:(FirstFrame *)firstFrame
{
_firstFrame = firstFrame;
//將計算的控制元件frame賦值
_lineView.frame = _firstFrame.lineViewFrame;
_titleLabel.frame = _firstFrame.titleLabelFrame;
_faceIageView.frame = _firstFrame.faceIageViewFrame;
}
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@classFirstData;
@interface FirstFrame : NSObject
@property (nonatomic,strong) FirstData *firstData;
@property (nonatomic,assign,readonly) CGRect lineViewFrame;
@property (nonatomic,assign,readonly) CGRect titleLabelFrame;
@property (nonatomic,assign,readonly) CGRect faceIageViewFrame;
@property (nonatomic,assign) CGFloat heightTableViewCellRow;//cell高度
@end
#import "FirstFrame.h"
#import "FirstData.h"
@implementation FirstFrame
-(void)setFirstData:(FirstData *)firstData//重寫set方法
{
_firstData = firstData;
CGRect rectname = [firstData.titleboundingRectWithSize:CGSizeMake(kScreenWidth-46, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOriginattributes:@{NSFontAttributeName:[UIFontsystemFontOfSize:15]}context:nil];
_titleLabelFrame = CGRectMake(34, 47-32, kScreenWidth-46, rectname.size.height);
_lineViewFrame = CGRectMake(16, 0, 1, 60+rectname.size.height+(kScreenWidth-46)*32/66.0-32);
_faceIageViewFrame = CGRectMake(34, 59 +rectname.size.height-32 , kScreenWidth-46, (kScreenWidth-46)*32/66.0);
//cell高度
_heightTableViewCellRow = 60+rectname.size.height+(kScreenWidth-46)*32/66.0-32;
}
@end
//不明白的歡迎私聊,指正