定义UITableView 并且遵守两个协议
firstTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight-64) style:UITableViewStylePlain];
firstTableView.separatorStyle = NO;
firstTableView.delegate = self;
firstTableView.dataSource = self;
[self.view addSubview:firstTableView];
//返回每个cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
FirstFrame *firstFrame = [frameArr objectAtIndex:indexPath.row];
return firstFrame.heightTableViewCellRow;
}
//cell的内容
/*
FirstData *firstData = [FirstData arrayWithDic:dic];//网络加载json解析为对象
FirstFrame *firstFrame = [[FirstFrame alloc]init];//计算cell高度的累
firstFrame.firstData = firstData;
[firstDataArr addObject:firstData];//数据类数组
[frameArr addObject:firstFrame];//cell高度类数组
*/- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
FirstTableViewCell *cell = [FirstTableViewCell theCorpusWithTableView: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 = [[FirstTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier: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.contentView addSubview:_lineView];
_timeBackView = [[UIView alloc]initWithFrame:CGRectMake(12, 15, 120, 17)];
[self.contentView addSubview:_timeBackView];
_titleLabel = [[UILabel alloc]init];
[self.contentView addSubview:_titleLabel];
_faceIageView = [[UIImageView alloc]init];
[self.contentView addSubview:_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>
@class FirstData;
@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.title boundingRectWithSize:CGSizeMake(kScreenWidth-46, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize: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
//不明白的欢迎私聊,指正