#import "RootViewController.h"
#import "MyCell.h"
@interface RootViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *picArr;
@property(nonatomic, retain)NSMutableArray *ziArr;
@end
@implementation RootViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.picArr = [NSMutableArray arrayWithObjects:@"h5.jpeg", @"BackGround.png", @"BZombie1.tiff", nil];
self.ziArr = [NSMutableArray arrayWithObjects:@"中国共产党新闻网北京4月1日电 (万鹏)3月28日,习近平主席出席2015年博鳌论坛年会开幕式并发表了题为《迈向命运共同体 开创亚洲新未来》的主旨演讲,他强调,“亚洲是世界的亚洲。亚洲要迈向命运共同体、开创亚洲新未来,必须在世界前进的步伐中前进、在世界发展的潮流中发展。习主席的演讲传递了哪些重要信息?国务院参事室特邀研究员保育钧,中国国际问题研究院研究员杨希雨做客人民网时谈到,习主席主旨演讲展现出“五大亮点”,再次提出“亚洲方式”的新命题,开幕式本身可谓“一带一路”的各国大合唱,让人印象深刻", @"床前明月光,疑是地上霜.举头望明月,低头思故乡", @"NBA常规赛强强对话,勇士在一度落后17分的情况下,客场以110-106逆转快船,终结对手7连胜的同时豪取10连胜。库里全场轰下27分,并在第二节运球晃倒保罗,技惊四座。快船格里芬40分,外加12篮板5助攻",nil];
}
return self;
}
- (void)dealloc
{
[_tableView release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
[self.view addSubview:self.tableView];
self.tableView.backgroundColor = [UIColor cyanColor];
[_tableView release];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.rowHeight = 80;
}
**cell自适应高度**
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIImage *image = [UIImage imageNamed:self.picArr[indexPath.row]];
CGFloat picHeight = self.view.frame.size.width * image.size.height / image.size.width;
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16], NSFontAttributeName, nil];
CGRect rect = [self.ziArr[indexPath.row] boundingRectWithSize:CGSizeMake(375, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
return picHeight + rect.size.height;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.picArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *reuse = @"reuse";
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
if (!cell) {
cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
}
cell.firstImageView.image = [UIImage imageNamed:self.picArr[indexPath.row]];
cell.textLabel.text = @"1";
cell.myLabel.text = self.ziArr[indexPath.row];
cell.detailTextLabel.text = @"12";
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
#import <UIKit/UIKit.h>
@interface MyCell : UITableViewCell
@property(nonatomic, retain)UIImageView *firstImageView;
@property(nonatomic, retain)UILabel *myLabel;
@end
#import "MyCell.h"
@implementation MyCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self createView];
}
return self;
}
- (void)createView
{
self.firstImageView = [[UIImageView alloc] init];
[self.contentView addSubview:self.firstImageView];
self.firstImageView.layer.borderWidth = 1;
self.firstImageView.layer.cornerRadius = 10;
[_firstImageView release];
self.myLabel = [[UILabel alloc] init];
[self.contentView addSubview:self.myLabel];
[_myLabel release];
self.myLabel.numberOfLines = 0;
self.myLabel.font = [UIFont systemFontOfSize:16];
}
- (void)layoutSubviews
{
[super layoutSubviews];
**自定义cell高度**
CGFloat picHeight = self.contentView.frame.size.width * self.firstImageView.image.size.height / self.firstImageView.image.size.width;
self.firstImageView.frame = CGRectMake(0, 0, self.contentView.frame.size.width, picHeight);
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16], NSFontAttributeName, nil];
CGRect rect = [self.myLabel.text boundingRectWithSize:CGSizeMake(375, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
self.myLabel.frame = CGRectMake(0, picHeight, self.contentView.frame.size.width, rect.size.height);
}
- (void)awakeFromNib {
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
}
@end