UI 10 cell 自定义高度
2015-08-29 10:59
507 查看
#import "MainViewController.h" #import "MyTableViewCell.h" @interface MainViewController ()<UITableViewDataSource, UITableViewDelegate> @property(nonatomic, retain)NSArray *picArr; @property(nonatomic, retain)NSMutableArray *ziArr; @end @implementation MainViewController - (void)dealloc{ [_picArr release]; [_ziArr release]; [super dealloc]; } - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.picArr = [NSArray arrayWithObjects:@"1.jpg", @"2.png", @"3.png",nil]; self.ziArr = [NSMutableArray arrayWithObjects:@"中国共产党新闻网北京4月1日电 (万鹏)3月28日,习近平主席出席2015年博鳌论坛年会开幕式并发表了题为《迈向命运共同体 开创亚洲新未来》的主旨演讲,他强调,“亚洲是世界的亚洲。亚洲要迈向命运共同体、开创亚洲新未来,必须在世界前进的步伐中前进、在世界发展的潮流中发展。习主席的演讲传递了哪些重要信息?国务院参事室特邀研究员保育钧,中国国际问题研究院研究员杨希雨做客人民网时谈到,习主席主旨演讲展现出“五大亮点”,再次提出“亚洲方式”的新命题,开幕式本身可谓“一带一路”的各国大合唱,让人印象深刻", @"床前明月光,疑是地上霜.举头望明月,低头思故乡", @"NBA常规赛强强对话,勇士在一度落后17分的情况下,客场以110-106逆转快船,终结对手7连胜的同时豪取10连胜。库里全场轰下27分,并在第二节运球晃倒保罗,技惊四座。快船格里芬40分,外加12篮板5助攻",nil]; } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.navigationController.navigationBar.translucent = NO; UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375, 667 - 64) style:UITableViewStylePlain]; tableView.backgroundColor = [UIColor orangeColor]; [self.view addSubview:tableView]; [tableView release]; tableView.delegate = self; tableView.dataSource = self; // 使用TableView的rowHeight也可以设置cell高,但是就固定了,不灵活. //因为viewDidLoad 就走一次. } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.picArr.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *reuse = @"reuse"; MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse]; if (!cell) { cell = [[[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse] autorelease]; } cell.myImageView.image = [UIImage imageNamed:self.picArr[indexPath.row]]; cell.labelMy.text = self.ziArr[indexPath.row]; return cell; } #pragma mark 这个方法是tableView的delegate所提供的协议方法,主要用来设置每一个行高. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ //根据图片尺寸设置行高 UIImage *image = [UIImage imageNamed:self.picArr[indexPath.row]]; //通过CGSize找到图片的尺寸 CGSize picSize = image.size; // 计算行高 CGFloat rowHeight = picSize.height * self.view.frame.size.width / picSize.width; //计算label高度 // 根据对应的文字求出cell上label显示的高度. NSMutableDictionary *fontDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14],NSFontAttributeName, nil]; //根据文字的大小,计算出文本的尺寸. //还需要一个尺寸(375,0); //第三个参数:计算高度需要依据字体的哪个特征来确定. CGRect rect = [self.ziArr[indexPath.row] boundingRectWithSize:CGSizeMake(375,0) options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDic context:nil]; //返回结果 return rowHeight + rect.size.height; }
#import "MyTableViewCell.h" @implementation MyTableViewCell - (void)dealloc{ [_myImageView release]; [super dealloc]; } - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { [self creat]; } return self; } - (void)creat{ self.myImageView = [[UIImageView alloc] init]; [self.contentView addSubview:self.myImageView]; self.myImageView.backgroundColor = [UIColor cyanColor]; [_myImageView release]; self.labelMy = [[UILabel alloc] init]; [self.contentView addSubview:self.labelMy]; self.labelMy.backgroundColor = [UIColor cyanColor]; [_labelMy release]; //指定label的字体大小. self.labelMy.font = [UIFont systemFontOfSize:14];//默认17号 self.labelMy.numberOfLines = 0; [self.labelMy sizeToFit]; } - (void)layoutSubviews{ // 让imageview 的尺寸和cell 的图片大小相同. // 因为这个方法是最后一个被执行的,所以执行到这个方法的时候,已经对cell的各个属性进行完赋值操作了,所以可以通过imageview.image找到图片尺寸 [super layoutSubviews]; CGSize picsize = self.myImageView.image.size; CGFloat height = picsize.height * self.contentView.frame.size.height / picsize.width; self.myImageView.frame = CGRectMake(0, 0, self.contentView.frame.size.width, height); //计算label高度 // 根据对应的文字求出cell上label显示的高度. NSMutableDictionary *fontDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14],NSFontAttributeName, nil]; //根据文字的大小,计算出文本的尺寸. //还需要一个尺寸(375,0); //第三个参数:计算高度需要依据字体的哪个特征来确定. CGRect rect = [self.labelMy.text boundingRectWithSize:CGSizeMake(375,0) options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDic context:nil]; self.labelMy.frame = CGRectMake(0, height, self.contentView.frame.size.width, rect.size.height); }
相关文章推荐
- UI 09 自定义cell
- 《第一行代码》读书笔记(二)----UI开发(上)
- 野人学Android基础篇之初探UI控件第六课--ListView和他的adapter们(一)
- UI 09 tableView 中国省市区. 一个页面, 三个tableView
- UI 08 tableView版中国省市区 -- 3页
- UI tableView 的头视图 & "小广告"
- Win10正式版怎么删除搜狗输入法中的SogouExe顽固文件?
- UVA 1605 Building for UN
- UI 07 界面传值 之 从后向前 ---- 协议传值
- HDU 5147 Sequence II (线段树求前缀和、后缀和)
- UIView的alpha、hidden和opaque属性之间的关系和区别
- C#基础----string和stringBuilder
- Win10 10525升级Win10 Build 10532失败回滚怎么办?
- POJ 3080 Blue Jeans(水~)
- 单行文本框和多行文本框UiTextfield UiTextView
- POJ 2871 A Simple Question of Chemistry(水~)
- POJ 2785 4 Values whose Sum is 0(hash表)
- 从数据库中提取到ueditor并且重新编辑
- POJ 2664 && HDU 1144 Prerequisites?(水~)
- 【LeetCode-面试算法经典-Java实现】【225-Implement Stack using Queues(用队列实现栈操作)】