iOS UI10_cell自适应高度
来源:互联网 发布:巨人网络财报 编辑:程序博客网 时间:2024/06/16 18:23
#import "MainViewController.h"#import "Cell.h"@interface MainViewController ()<UITableViewDataSource, UITableViewDelegate>@property(nonatomic, retain)NSArray *picArr;@property(nonatomic, retain)NSMutableArray *ziArr;@end@implementation MainViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.navigationController.navigationBar.translucent = NO; self.view.backgroundColor = [UIColor greenColor]; self.navigationItem.title=@"心静自然凉"; UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain]; tableView.backgroundColor=[UIColor yellowColor]; [self.view addSubview:tableView];// [tableView setEditing:YES animated:YES]; [tableView release]; tableView.delegate = self; tableView.dataSource = self;// //他只会执行一次,可以设置行高,但是不灵活// tableView.rowHeight=100;}- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { [self create]; } return self;}- (void)create{ self.picArr = [NSArray arrayWithObjects:@"1.jpg", @"2.jpg", @"3.jpg",nil]; self.ziArr = [NSMutableArray arrayWithObjects:@"中国共产党新闻网北京4月1日电 (万鹏)3月28日,习近平主席出席2015年博鳌论坛年会开幕式并发表了题为《迈向命运共同体 开创亚洲新未来》的主旨演讲,他强调,“亚洲是世界的亚洲。亚洲要迈向命运共同体、开创亚洲新未来,必须在世界前进的步伐中前进、在世界发展的潮流中发展。习主席的演讲传递了哪些重要信息?国务院参事室特邀研究员保育钧,中国国际问题研究院研究员杨希雨做客人民网时谈到,习主席主旨演讲展现出“五大亮点”,再次提出“亚洲方式”的新命题,开幕式本身可谓“一带一路”的各国大合唱,让人印象深刻", @"床前明月光,疑是地上霜.举头望明月,低头思故乡", @"NBA常规赛强强对话,勇士在一度落后17分的情况下,客场以110-106逆转快船,终结对手7连胜的同时豪取10连胜。库里全场轰下27分,并在第二节运球晃倒保罗,技惊四座。快船格里芬40分,外加12篮板5助攻",nil];}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 3;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *reuse = @"reuse"; Cell *cell = [tableView dequeueReusableCellWithIdentifier:reuse]; if (!cell) { cell = [[Cell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse]; } cell.cellImageView.image=[UIImage imageNamed:self.picArr[indexPath.row]]; cell.newLabel.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找到image里面图片的尺寸 CGSize picSize=image.size; //计算行高 CGFloat rowHeight = picSize.height * self.view.frame.size.width / picSize.width; //计算label高 //根据对应的文字求出cell上的label显示的高度 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;}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
//// Cell.h// UI10_cell自适应高度//// Created by dllo on 15/8/11.// Copyright (c) 2015年 zhozhicheng. All rights reserved.//#import <UIKit/UIKit.h>@interface Cell : UITableViewCell@property(nonatomic,retain)UIImageView *cellImageView;@property(nonatomic,retain)UILabel *newLabel;@end
//// Cell.m// UI10_cell自适应高度//// Created by dllo on 15/8/11.// Copyright (c) 2015年 zhozhicheng. All rights reserved.//#import "Cell.h"@implementation Cell-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { [self createView]; } return self;}-(void)createView{ self.cellImageView=[[UIImageView alloc] init]; self.cellImageView.backgroundColor=[UIColor yellowColor]; [self.contentView addSubview:self.cellImageView]; [_cellImageView release]; self.newLabel=[[UILabel alloc] init]; self.newLabel.backgroundColor=[UIColor yellowColor]; //指定label的字体大小 //默认字体是17 self.newLabel.font=[UIFont systemFontOfSize:14]; self.newLabel.numberOfLines=0; [self.newLabel sizeToFit]; [self.contentView addSubview:self.newLabel]; [_newLabel release];}-(void)dealloc{ [_cellImageView release]; [_newLabel release]; [super dealloc];}-(void)layoutSubviews{ [super layoutSubviews]; //让imageView尺寸和图片大小相同 //因为这个方法是最后一个执行的,所以执行到这个方法的时候,已经对cell各个属性完成赋值操作,所以可以通过imageView.image找到图片的尺寸 CGSize picSize = self.cellImageView.image.size; CGFloat height = picSize.height * self.contentView.frame.size.width / picSize.width; self.cellImageView.frame =CGRectMake(0, 0, self.contentView.frame.size.width, height); NSDictionary *fontDic=[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14],NSFontAttributeName, nil]; //根据文字的大小,计算出文本的尺寸 //还需要执行一个尺寸(375,0) //第三个参数,计算高度需要根据字体的哪个特征来确定 CGRect rect = [self.newLabel.text boundingRectWithSize:CGSizeMake(375, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDic context:nil]; self.newLabel.frame=CGRectMake(0, height, self.contentView .frame.size.width, rect.size.height);}- (void)awakeFromNib { // Initialization code}- (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state}@end
0 0
- iOS UI10_cell自适应高度
- UI10_cell的自适应高度
- UI10_cell自适应高度(显示尺寸不同图片.长短不一的字符串)
- iOS - UITableViewCell自适应高度
- IOS UITextView自适应高度
- iOS Cell高度自适应
- IOS UITextView自适应高度
- iOS:UITextView自适应高度
- IOS TextView 自适应高度
- IOS Label 自适应高度
- IOS UITextView自适应高度
- IOS文本自适应高度
- ios cell自适应高度
- iOS 自适应label高度
- iOS UITableCell自适应高度
- iOS键盘自适应高度
- IOS UITextView自适应高度
- IOS tableView自适应高度
- phaser教程1
- 图算法
- Android之——NDK环境搭建
- 黑马程序员------------------Java基础----------------字符串
- MySQL数据库
- iOS UI10_cell自适应高度
- class中的field,property,method,constructor
- 116. Populating Next Right Pointers in Each Node
- 企业源代码保护,软件公司源代码管理
- S7-200与紫金桥Modbus从机的通讯
- hadoop学习笔记 Hive和普通关系数据库比较
- 三、晓凡调试-Android开发版之应用调试
- 1030. Travel Plan (30) - Dijkstra
- 与数组相关算法