自适应高度
2015-12-27 15:21
363 查看
1.MyCell.h
2.MyCell.m
3.RootViewController.m
#import <UIKit/UIKit.h> @interface MyCell : UITableViewCell @property(nonatomic,retain)UIImageView *picImageView; @property(nonatomic,retain)UILabel *newsLabel; @end
2.MyCell.m
@implementation MyCell -(void)dealloc{ [_picImageView release]; [super dealloc]; } -(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { [self createView]; } return self; } -(void)createView{ self.picImageView=[[UIImageView alloc] init]; self.picImageView.backgroundColor=[UIColor redColor]; [self.contentView addSubview:self.picImageView]; [self.picImageView release]; self.newsLabel=[[UILabel alloc] init]; [self.contentView addSubview:self.newsLabel]; [_newsLabel release]; } -(void)layoutSubviews{ [super layoutSubviews]; //这个是cell显示之前走得最后一个方法 UIImage *image = self.picImageView.image; CGFloat height=image.size.height*self.contentView.frame.size.width/image.size.width; self.picImageView.frame=CGRectMake(0, 0, self.contentView.frame.size.width, height); //计算label高度 self.newsLabel.numberOfLines=0; self.newsLabel.font=[UIFont systemFontOfSize:14]; NSString *str=self.newsLabel.text; NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14], NSFontAttributeName, nil]; // 根据文本的内容和文本的字体进行计算高度 // 参数1. 告诉系统, 文本显示的最大范围 CGRect rect = [str boundingRectWithSize:CGSizeMake(self.contentView.frame.size.width, 0) options:1<<0 attributes:dic context:nil]; //对label进行尺寸设置 self.newsLabel.frame=CGRectMake(0, height, self.contentView.frame.size.width, rect.size.height); }
3.RootViewController.m
#import "RootViewController.h" #import "MyCell.h" /*和本题无关(按位左移) typedef NS_ENUM(NSUInteger, NSMyEnum) { NSTestFirst=1<<0, NSTestSecond =1<<1, NSTestThird=1<<2, NSTestFouth=1<<3, }; */ @interface RootViewController ()<UITableViewDataSource,UITableViewDelegate> @property(nonatomic,retain)UITableView *tableView; @property(nonatomic,retain)NSArray *picArr; @property(nonatomic,retain)NSMutableArray *ziArr; @end @implementation RootViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor=[UIColor cyanColor]; self.navigationController.navigationBar.translucent=NO; self.picArr=@[@"c2.jpg",@"c4.jpg",@"c6.jpg"]; self.ziArr = [NSMutableArray arrayWithObjects:@"中国共产党新闻网北京4月1日电(万鹏)3月28日,习近平主席出席2015年博鳌论坛年会开幕式并发表了题为《迈向命运共同体 开创亚洲新未来》的主旨演讲,他强调,“亚洲是世界的亚洲。亚洲要迈向命运共同体、开创亚洲新未来,必须在世界前进的步伐中前进、在世界发展的潮流中发展。习主席的演讲传递了哪些重要信息?国务院参事室特邀研究员保育钧,中国国际问题研究院研究员杨希雨做客人民网时谈到,习主席主旨演讲展现出“五大亮点”,再次提出“亚洲方式”的新命题,开幕式本身可谓“一带一路”的各国大合唱,让人印象深刻", @"床前明月光,疑是地上霜.举头望明月,低头思故乡", @"NBA常规赛强强对话,勇士在一度落后17分的情况下,客场以110-106逆转快船,终结对手7连胜的同时豪取10连胜。库里全场轰下27分,并在第二节运球晃倒保罗,技惊四座。快船格里芬40分,外加12篮板5助攻",nil]; self.tableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-64) style:UITableViewStylePlain]; [self.view addSubview:self.tableView]; [self.tableView release]; self.tableView.dataSource = self; self.tableView.delegate = self; NSLog(@"%ld",NSTestFirst|NSTestFouth); } - (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] autorelease]; } cell.picImageView.image=[UIImage imageNamed:self.picArr[indexPath.row]]; cell.newsLabel.text=self.ziArr[indexPath.row]; return cell; } #pragma mark 设置tableView每行的高度 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ //在这个方法里,主要计算图片的尺寸,来设置tableView的行高 UIImage *image=[UIImage imageNamed:self.picArr[indexPath.row]]; //UIImage保存着实际图片的尺寸,而UIImageView是我们看见的图片的尺寸都通过它来设置 //通过图片的实际尺寸和屏幕固定的宽进行等比例计算 CGFloat rowHeight=self.view.frame.size.width * image.size.height /image.size.width; //计算文字的高度 // 计算每一个字的大小 NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14], NSFontAttributeName, nil]; // 根据文本的内容和文本的字体进行计算高度 // 参数1. 告诉系统, 文本显示的最大范围 CGRect rect = [self.ziArr[indexPath.row]boundingRectWithSize:CGSizeMake(self.view.frame.size.width, 0) options:1<<0 attributes:dic context:nil]; // 然后把图片的高和文本的高为返回值返回 return rowHeight + rect.size.height; }
相关文章推荐
- GIS的刊物
- xcode7 Enable Zombie Objects 没反应...
- 将字符串 数组 字典写入本地文件,并计算文件的大小,最后删除文件
- 第一类Stirling数和第二类Stirling数
- Servlet
- 【码农流水账】一个码农的成长02
- WebService到底是什么?(转)
- LeetCode Spiral Matrix II
- Linux软件安装之RPM
- 自动化管理工具Saltstack之用户管理篇(6)
- Makefile 使用总结
- 排列组合相关算法 python
- WKWebView显示空白页bug
- Oracle GoldenGate for Big Data 12.2.0.1的新特性
- 有n个整数,指定位置m处插入g个值(用指针和函数)
- Linux网络编程入门
- 056_最长公共子序列
- (Frontend Newbie)Web简史
- 使用Maven Profile按需加载开发、测试和产品环境的配置文件
- cpio命令用法