RootTableViewController.m
#import "RootTableViewController.h"
#import "TextTableViewCell.h"
#import "ImageTableViewCell.h"
#import "Model.h"
@interface RootTableViewController ()
//存放数据
@property (nonatomic,strong) NSMutableArray * dataArray;
@end
@implementation RootTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 注册
// cell的注册,可以注册多个,但是重用标识不能重复
[self.tableView registerClass:[TextTableViewCell class] forCellReuseIdentifier:@"Text"];
[self.tableView registerClass:[ImageTableViewCell class] forCellReuseIdentifier:@"Image"];
// 制作数据
self.dataArray = [NSMutableArray array];
Model * m1 = [[Model alloc]init];
m1.type = @"Text";
m1.text = @"wfwafwefafewfaggbasvagagaggagaegaggggaagasdasdsadasddadas";
Model * m2 = [[Model alloc] init];
m2.type = @"Image";
m2.image = [UIImage imageNamed:@"hehe.png"];
Model * m3 = [[Model alloc]init];
m3.type = @"Text";
m3.text = @"bbbbbbbbbb";
[self.dataArray addObject:m1];
[self.dataArray addObject:m2];
[self.dataArray addObject:m3];
// 计算文字高度
NSString * s = @"aaaaaaaaaUasdgdsgadgdgsagagdasgdgsagagdasgdgsagagdasggagadggagd";
// 第一个属性:宽和高:宽最宽多少,最高多少(不能超过的值),一般给个大值
CGRect r = [s boundingRectWithSize:CGSizeMake(150, 2000) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17.0]} context:nil];
NSLog(@"%@",NSStringFromCGRect(r));
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.dataArray.count;
}
//创建cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// 获取数据
Model * m = self.dataArray[indexPath.row];
// 判断类型
if ([m.type isEqualToString:@"Text"]) {
TextTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"Text" forIndexPath:indexPath];
cell.lab.text = m.text;
// 改变label高度
// CGRect temp = cell.lab.frame;
// temp.size.height =[self p_heightWithString:m.text];
// cell.lab.frame = temp;
return cell;
}else if( [m.type isEqualToString:@"Image"]){
ImageTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"Image" forIndexPath:indexPath];
cell.imV.image = m.image;
return cell;
}
return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
// 取出model对象,获得type判断
Model * m = self.dataArray[indexPath.row];
// 如果是TextTableView,则计算高度
if ([m.type isEqualToString:@"Text"]) {
CGFloat f = [self p_heightWithString:m.text]/0.8;
return f;
}
return 100;
}
//输入一段文字,返回该文字高度
-(CGFloat)p_heightWithString:(NSString *)aString{
CGRect r = [aString boundingRectWithSize:CGSizeMake([TextTableViewCell backWeight], 2000) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:18.0f]} context:nil];
// return aString.size.height;
return r.size.height;
}
@end
TextTableViewCell.h
#import <UIKit/UIKit.h>
@interface TextTableViewCell : UITableViewCell
@property (nonatomic,strong ) UILabel * lab;
//返回cell中固定高度
//+(CGFloat)backHeight;
//返回宽度
+(CGFloat)backWeight;
@end
TextTableViewCell.m
#import "TextTableViewCell.h"
#define kWidth self.contentView.frame.size.width
#define kHeight self.contentView.frame.size.height
@implementation TextTableViewCell
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
[self p_setupView];
}
return self;
}
-(void)p_setupView{
self.lab = [[UILabel alloc]init];
_lab.frame = CGRectMake(kWidth*0.1, kHeight*0.1, kWidth*0.8, kHeight*0.8);
_lab.backgroundColor = [UIColor grayColor];
// 自动折行
self.lab.numberOfLines = 0;
[self.contentView addSubview:_lab];
}
-(void)layoutSubviews{
[super layoutSubviews];
_lab.frame = CGRectMake(kWidth*0.1, kHeight*0.1, kWidth*0.8, kHeight*0.8);
}
+(CGFloat)backWeight{
return [UIScreen mainScreen].bounds.size.width * 0.8;
}
@end
ImageTableViewCell.h
#import <UIKit/UIKit.h>
@interface ImageTableViewCell : UITableViewCell
@property (nonatomic,strong) UIImageView * imV;
@end
ImageTableViewCell.m
#import "ImageTableViewCell.h"
#define kWidth self.contentView.frame.size.width
#define kHeight self.contentView.frame.size.height
@implementation ImageTableViewCell
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
[self p_setupViews];
}
return self;
}
-(void)p_setupViews{
_imV = [[UIImageView alloc] init];
_imV.frame = CGRectMake(kWidth*0.1, kHeight*0.1, kWidth*0.8, kHeight*0.8);
_imV.backgroundColor = [UIColor grayColor];
[self.contentView addSubview:_imV];
}
-(void)layoutSubviews{
[super layoutSubviews];
_imV.frame = CGRectMake(kWidth*0.1, kHeight*0.1, kWidth*0.8, kHeight*0.8);
}
@end
Model.h
#import <Foundation/Foundation.h>
//UI框架
#import <UIKit/UIKit.h>
@interface Model : NSObject
//给textTableviewCell使用
@property (nonatomic,copy) NSString * text;
//给ImageTableViewCell使用
@property (nonatomic,strong) UIImage * image;
//设置类型
@property (nonatomic,copy)NSString * type;
@end
Model.m
#import "Model.h"
@implementation Model
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
}
@end