UI10_cell自适应高度(显示尺寸不同图片.长短不一的字符串)
来源:互联网 发布:人知从太守游而乐的而 编辑:程序博客网 时间:2024/09/21 09:22
目的:就是在tableView添加三个尺寸不同字符串(初始化时设置的),还有三个尺寸不同的图片(在viewDidLoad里创建的)
在主视图控制器.m文件中
1.首先定义初始化方法,如下:-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.picArr = [NSMutableArray arrayWithObjects:@"01.png", @"02.png", @"03.png",nil]; self.ziArr = [NSMutableArray arrayWithObjects:@"中国共产党新闻网北京4月1日电 (万鹏)3月28日,习近平主席出席2015年博鳌论坛年会开幕式并发表了题为《迈向命运共同体 开创亚洲新未来》的主旨演讲,他强调,“亚洲是世界的亚洲。亚洲要迈向命运共同体、开创亚洲新未来,必须在世界前进的步伐中前进、在世界发展的潮流中发展。习主席的演讲传递了哪些重要信息?国务院参事室特邀研究员保育钧,中国国际问题研究院研究员杨希雨做客人民网时谈到,习主席主旨演讲展现出“五大亮点”,再次提出“亚洲方式”的新命题,开幕式本身可谓“一带一路”的各国大合唱,让人印象深刻", @"床前明月光,疑是地上霜.举头望明月,低头思故乡", @"NBA常规赛强强对话,勇士在一度落后17分的情况下,客场以110-106逆转快船,终结对手7连胜的同时豪取10连胜。库里全场轰下27分,并在第二节运球晃倒保罗,技惊四座。快船格里芬40分,外加12篮板5助攻",nil]; } return self;}
2.创建tableView
UITableView *tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-64) style:UITableViewStylePlain]; tableView.dataSource=self; tableView.delegate=self; [self.view addSubview:tableView]; [tableView release]; //viewDidLoad方法只会执行一次,我们如果设置行高,就会将行高定死,不灵活此外还有它必须实现的方法 下面我们会进行cell的自定义 那么关于cell的协议里我们会将系统的cell替换掉关于cell协议设置的内容-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{static NSString *reuse=@"reuse"; Cell1 *cell=[tableView dequeueReusableCellWithIdentifier:reuse]; if (!cell) { cell=[[Cell1 alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse]; } NSString *picName=self.picArr[indexPath.row]; cell.imageOne.image=[UIImage imageNamed:picName]; ; cell.myLabel.text=self.ziArr[indexPath.row]; return cell;}
3.设置每一行的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ //根据图片的尺寸设置行高 UIImage *image=[UIImage imageNamed:self.picArr[indexPath.row]]; //通过CGSize找到image里面的图片的尺寸 CGSize picSize=image.size; //计算行高 //等比例放大后rowHeight是多少 CGFloat rowHeight=picSize.height*self.view.frame.size.width/picSize.width; //计算label的高度 字号要统一 //根据对应的文字求出cell上label显示的高度 //NSFontAttributeName:常量字符串 NSDictionary *fontDic=[NSDictionary 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;}
自定义cell部分
在.h文件中我们会设置一个UIImageView类型和UILable类型的属性下面是在.m文件中的内容1.初始化自定义-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { //完成对属性视图的创建,但是一般创建之后不给属性视图frame [self createView]; } return self;}2.自定义中的方法实现部分-(void)createView{ self.imageOne=[[UIImageView alloc]init]; self.imageOne.backgroundColor=[UIColor redColor]; [self.contentView addSubview:self.imageOne]; [self.imageOne release]; self.myLabel=[[UILabel alloc]init]; //指定label的字体大小 //字体系统默认17号 self.myLabel.font=[UIFont systemFontOfSize:14]; //0是最大值 self.myLabel.numberOfLines=0; [self.myLabel sizeToFit]; [self.contentView addSubview:self.myLabel]; [_myLabel release];}3.-(void)layoutSubviews{ [super layoutSubviews]; //让imageView尺寸和image的图片大小相同 //因为这个方法是最后一个执行,所以执行到这个方法的时候,已经对cell的各个属性进行完赋值操作,所以可以通过imageView.image找到图片的尺寸大小 CGSize picSize = self.imageOne.image.size; CGFloat height=self.contentView.frame.size.width*picSize.height/picSize.width; self.imageOne.frame=CGRectMake(0, 0, self.contentView.frame.size.width, height); NSDictionary *fontDic=[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14],NSFontAttributeName, nil]; CGRect rect=[self.myLabel.text boundingRectWithSize:CGSizeMake(375, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDic context:nil]; self.myLabel.frame=CGRectMake(0, height, self.contentView.frame.size.width, rect.size.height);}4.内存管理- (void)dealloc{ [_imageOne release]; [super dealloc];}
0 0
- UI10_cell自适应高度(显示尺寸不同图片.长短不一的字符串)
- UI10_cell的自适应高度
- iOS UI10_cell自适应高度
- UITableView的图片自适应高度
- 实现窗体自适应调整尺寸以适应不同屏幕分辩率的显示问题
- Adapter中按比例显示不同高度的图片
- 自适应iPhone的不同键盘高度
- 自适应iPhone的不同键盘高度
- 自适应iPhone的不同键盘高度
- 自适应iPhone的不同键盘高度
- 限制图片显示的尺寸
- 纯图片高度自适应圆角框的制作
- 移动web图片高度自适应的解决方案
- 如何是使EVC应用程序自适应不同尺寸的LCD?
- 如何是使EVC应用程序自适应不同尺寸的LCD?
- jmacick生成不同尺寸图片的应用
- Matlab拼接(cat)两副尺寸不同的图片,并显示
- 图片宽高度自适应
- apue学习笔记(第十一章 线程)
- apue学习笔记(第十二章 线程控制)
- 1055. The World's Richest (25)
- apue学习笔记(第十三章 守护进程)
- apue学习笔记(第十四章 高级I/O)
- UI10_cell自适应高度(显示尺寸不同图片.长短不一的字符串)
- apue学习笔记(第十五章 进程间通信)
- apue学习笔记(第十六章 网络IPC:套接字)
- apue学习笔记(第十七章 高级进程间通信)
- Eddy's picture
- c++引用总结
- TCP/IP详解 卷一(第一章 概述)
- TCP/IP详解 卷一(第二章 链路层)
- TCP/IP详解 卷一(第三章 IP:网际协议)