[PS解释图文混排LS](https://pan.baidu.com/s/1jIHdTaA 密码 wkg9)
// ViewController.m
// 测试cell的高度自适应
//
// Created by stone on 16/6/5.
// Copyright © 2016年 zm. All rights reserved.
//
#import "ViewController.h"
#import "Model.h"
#import "MyTableViewCell.h"
#import "ZmImageView.h"
@interface ViewController ()
@property (nonatomic,strong)UITableView* tableView;
@property (nonatomic,strong)NSMutableArray* dataArr;
@end
@implementation ViewController
-(NSMutableArray *)dataArr
{
if (_dataArr == nil) {
_dataArr = [[NSMutableArray alloc]init];
Model* model1 = [Model new];
model1.title = @"胖虎";
model1.content = @"我是胖虎我怕谁!我是胖虎我怕谁!我我是胖虎我怕谁!我是胖虎我怕谁!我是胖虎我怕谁!是胖虎我怕谁!";
model1.iconPath = [[NSBundle mainBundle]pathForResource:@"11" ofType:@"jpg"];
model1.image1 = [UIImage imageNamed:@"22.jpg"];
model1.image2 = [UIImage imageNamed:@"44.jpg"];
Model* model2 = [Model new];
model2.title = @"多啦A梦";
model2.content = @"我是多啦A梦我我是多啦A梦我我是多啦A梦我我是多啦A梦我我是多啦A梦我我是多啦A梦我我是多啦A梦我我是多啦A梦我我是多啦A梦我我是多啦A梦我我是多啦A梦我我是";
model2.iconPath =[[NSBundle mainBundle]pathForResource:@"11" ofType:@"jpg"];
model2.image1 = [UIImage imageNamed:@"2"];
model2.image2 = [UIImage imageNamed:@"11"];
model2.image1 = [UIImage imageNamed:@"33.jpg"];
model2.image2 = [UIImage imageNamed:@"22.jpg"];
Model* model3 = [Model new];
model3.title = @"大雄";
model3.content = @"我是大熊";
model3.iconPath = [[NSBundle mainBundle]pathForResource:@"11" ofType:@"jpg"];
[_dataArr addObject:model1];
[_dataArr addObject:model2];
[_dataArr addObject:model3];
}
return _dataArr;
}
-(UITableView *)tableView
{
if (_tableView == nil) {
_tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
// _tableView.separatorInset = UIEdgeInsetsMake(0,70, 0, 0);
_tableView.estimatedRowHeight = 50;
}
return _tableView;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"测试布局";
[self.view addSubview:self.tableView];
self.tableView.tableFooterView = [UIView new];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - UItabelViewDelegate UItableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString* identifier = @"cell";
MyTableViewCell* cell = [self.tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[MyTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
Model* model = [self.dataArr objectAtIndex:indexPath.row];
cell.titleLb.text = model.title;
cell.contentLb.text = model.content;
cell.imageView1.imageView.image = model.image1;
cell.imageView2.imageView.image = model.image2;
[cell.iconImageView setImage:[UIImage imageNamed:@"11.jpg"]];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
cell.separatorInset = UIEdgeInsetsZero;
cell.layoutMargins = UIEdgeInsetsZero;
cell.preservesSuperviewLayoutMargins = NO;
}
@end
//
// MyTableViewCell.h
// 测试cell的高度自适应
//
// Created by stone on 16/6/5.
// Copyright © 2016年 zm. All rights reserved.
//
#import
#import "ZmImageView.h"
@interface MyTableViewCell : UITableViewCell
@property (nonatomic,strong)UILabel* titleLb;
@property (nonatomic,strong)UILabel* contentLb;
@property (nonatomic,strong)UIImageView* iconImageView;
@property (nonatomic,strong)ZmImageView* imageView1;
@property (nonatomic,strong)ZmImageView* imageView2;
@property (nonatomic,strong)UIView* showPhotoView;
@end
// MyTableViewCell.m
// 测试cell的高度自适应
//
// Created by stone on 16/6/5.
// Copyright © 2016年 zm. All rights reserved.
//
#import "MyTableViewCell.h"
#import "Masonry.h"
#import "Model.h"
@implementation MyTableViewCell
- (void)awakeFromNib {
[super awakeFromNib];
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
[self initLayuot];
// self.backgroundColor = [UIColor blueColor];
self.selectionStyle = UITableViewCellSelectionStyleDefault;
// UIColor *color = [UIColor redColor];//通过RGB来定义自己的颜色
self.selectedBackgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"44.jpg"]];
// self.selectedBackgroundView.backgroundColor = color;
}
return self;
}
-(void)initLayuot{
self.titleLb = [UILabel new];
self.titleLb.backgroundColor = [UIColor grayColor];
self.contentLb = [UILabel new];
self.contentLb.numberOfLines = 0;
self.contentLb.font = [UIFont systemFontOfSize:14];
self.iconImageView = [UIImageView new];
self.imageView1 = [ZmImageView new];
self.imageView2 = [ZmImageView new];
self.imageView1.contentMode = UIViewContentModeScaleAspectFill;
self.imageView2.contentMode = UIViewContentModeScaleAspectFill;
[self.contentView addSubview:_contentLb];
[self.contentView addSubview:_iconImageView];
[self.contentView addSubview:_titleLb];
[self.contentView addSubview:_imageView1];
[self.contentView addSubview:_imageView2];
[self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(50, 50));
make.top.left.mas_equalTo(10);
}];
[self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
make.topMargin.mas_equalTo(self.iconImageView.mas_topMargin);
make.left.mas_equalTo(self.iconImageView.mas_right).mas_equalTo(10);
make.height.mas_equalTo(30);
}];
[self.contentView addSubview:_contentLb];
_contentLb.backgroundColor = [UIColor yellowColor];
[self.contentLb mas_makeConstraints:^(MASConstraintMaker *make) {
make.leftMargin.mas_equalTo(self.titleLb.mas_leftMargin );
make.right.mas_lessThanOrEqualTo(-20);
make.top.mas_equalTo(self.titleLb.mas_bottom).mas_equalTo(10);
make.bottom.mas_equalTo(self.imageView1.mas_top).mas_equalTo(-10);
make.height.mas_greaterThanOrEqualTo(3);
}];
[self.imageView1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.mas_greaterThanOrEqualTo(0);
make.height.mas_lessThanOrEqualTo(100);
make.left.mas_equalTo(10);
make.right.mas_equalTo(self.imageView2.mas_left).mas_equalTo(-10);
make.bottom.mas_equalTo(-10);
}];
[self.imageView2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(self.imageView1);
make.right.mas_equalTo(-10);
make.bottomMargin.mas_equalTo(self.imageView1.mas_bottomMargin);
}];
}
@end
// Model.h
// 测试cell的高度自适应
//
// Created by stone on 16/6/5.
// Copyright © 2016年 zm. All rights reserved.
//
#import
#import
@interface Model : NSObject
@property (nonatomic,strong)NSString* title;
@property (nonatomic,strong)NSString* content;
@property (nonatomic,strong)NSString* iconPath;
@property (nonatomic,strong)UIImage* image1;
@property (nonatomic,strong)UIImage* image2;
@end
//
// ZmImageView.h
// BaseProject
//
// Created by stone on 16/6/1.
// Copyright © 2016年 Tarena. All rights reserved.
//
#import
@interface ZmImageView : UIView
@property (nonatomic,strong)UIImageView* imageView;
@end
//
// ZmImageView.m
// BaseProject
//
// Created by stone on 16/6/1.
// Copyright © 2016年 Tarena. All rights reserved.
//
#import "ZmImageView.h"
#import "Masonry.h"
@implementation ZmImageView
-(instancetype)init
{
self = [super init];
if (self) {
_imageView = [UIImageView new];
[self addSubview:_imageView];
_imageView.contentMode = UIViewContentModeScaleAspectFill;
[_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(0);
}];
self.clipsToBounds = YES;
}
return self;
}
@end