需求:

1、表格里的UILable要求自动换行

2、创建的tableViewCell的高度会自动适应内容的高度

一、用xcode构建项目,创建一个有tableView的视图,用纯代码的形式实现:

1、创建一个UIViewController类,定义一个UITableView,实现TableView的委托和数据源协议

[objc] view plaincopyprint?
  1. //
  2. //  TableViewController.h
  3. //  AdaptiveCell
  4. //
  5. //  Created by swinglife on 14-1-10.
  6. //  Copyright (c) 2014年 swinglife. All rights reserved.
  7. //
  8. #import <UIKit/UIKit.h>
  9. @interface TableViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{
  10. }
  11. @property (nonatomic,retain) UITableView *tableView;
  12. @end

2、实现UITableView对象的初始化,声明一个tableData的数组对象用来保存tableView中得数据

[objc] view plaincopyprint?
  1. #import "TableViewController.h"
  2. @interface TableViewController (){
  3. NSMutableArray *tableData;  //tableView数据存放数组
  4. }
  5. @end
  6. @implementation TableViewController
  7. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  8. {
  9. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  10. if (self) {
  11. tableData = [[NSMutableArray alloc] init];
  12. }
  13. return self;
  14. }
  15. - (void)viewDidLoad
  16. {
  17. [super viewDidLoad];
  18. [self initTableView];
  19. }
  20. //初始化tableView;
  21. -(void)initTableView{
  22. CGRect frame = self.view.frame;
  23. _tableView = [[UITableView alloc] initWithFrame:CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height)];
  24. //代理类
  25. _tableView.delegate = self;
  26. //数据源
  27. _tableView.dataSource = self;
  28. [self.view addSubview:_tableView];
  29. }

3、创建自定义的UITableViewCell类,声明需要用到的控件对象和方法:

[objc] view plaincopyprint?
  1. #import <UIKit/UIKit.h>
  2. @interface TableViewCell : UITableViewCell{
  3. }
  4. //用户名
  5. @property(nonatomic,retain) UILabel *name;
  6. //用户介绍
  7. @property(nonatomic,retain) UILabel *introduction;
  8. //用户头像
  9. @property(nonatomic,retain) UIImageView *userImage;
  10. //给用户介绍赋值并且实现自动换行
  11. -(void)setIntroductionText:(NSString*)text;
  12. //初始化cell类
  13. -(id)initWithReuseIdentifier:(NSString*)reuseIdentifier;
  14. @end

4、初始化Cell的用户控件,实现方法:

[objc] view plaincopyprint?
  1. //
  2. //  TableViewCell.m
  3. //  AdaptiveCell
  4. //
  5. //  Created by swinglife on 14-1-10.
  6. //  Copyright (c) 2014年 swinglife. All rights reserved.
  7. //
  8. #import "TableViewCell.h"
  9. @implementation TableViewCell
  10. -(id)initWithReuseIdentifier:(NSString*)reuseIdentifier{
  11. self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
  12. if (self) {
  13. [self initLayuot];
  14. }
  15. return self;
  16. }
  17. //初始化控件
  18. -(void)initLayuot{
  19. _name = [[UILabel alloc] initWithFrame:CGRectMake(71, 5, 250, 40)];
  20. [self addSubview:_name];
  21. _userImage = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 66, 66)];
  22. [self addSubview:_userImage];
  23. _introduction = [[UILabel alloc] initWithFrame:CGRectMake(5, 78, 250, 40)];
  24. [self addSubview:_introduction];
  25. }
  26. //赋值 and 自动换行,计算出cell的高度
  27. -(void)setIntroductionText:(NSString*)text{
  28. //获得当前cell高度
  29. CGRect frame = [self frame];
  30. //文本赋值
  31. self.introduction.text = text;
  32. //设置label的最大行数
  33. self.introduction.numberOfLines = 10;
  34. CGSize size = CGSizeMake(300, 1000);
  35. CGSize labelSize = [self.introduction.text sizeWithFont:self.introduction.font constrainedToSize:size lineBreakMode:NSLineBreakByClipping];
  36. self.introduction.frame = CGRectMake(self.introduction.frame.origin.x, self.introduction.frame.origin.y, labelSize.width, labelSize.height);
  37. //计算出自适应的高度
  38. frame.size.height = labelSize.height+100;
  39. self.frame = frame;
  40. }
  41. - (void)setSelected:(BOOL)selected animated:(BOOL)animated
  42. {
  43. [super setSelected:selected animated:animated];
  44. }
  45. @end

5、现在需要一个存放数据对象的模型类,创建一个UserModel用来封装数据

[objc] view plaincopyprint?
  1. #import <Foundation/Foundation.h>
  2. @interface UserModel : NSObject
  3. //用户名
  4. @property (nonatomic,copy) NSString *username;
  5. //介绍
  6. @property (nonatomic,copy) NSString *introduction;
  7. //头像图片路径
  8. @property (nonatomic,copy) NSString *imagePath;
  9. @end

6、现在需要一些数据,在TableViewController.m文件中实现数据的创建:

[objc] view plaincopyprint?
  1. //我需要一点测试数据,直接复制老项目东西
  2. -(void)createUserData{
  3. UserModel *user = [[UserModel alloc] init];
  4. [user setUsername:@"胖虎"];
  5. [user setIntroduction:@"我是胖虎我怕谁!!我是胖虎我怕谁!!我是胖虎我怕谁!!"];
  6. [user setImagePath:@"panghu.jpg"];
  7. UserModel *user2 = [[UserModel alloc] init];
  8. [user2 setUsername:@"多啦A梦"];
  9. [user2 setIntroduction:@"我是多啦A梦我有肚子!!我是多啦A梦我有肚子!!我是多啦A梦我有肚子!!我是多啦A梦我有肚子!!我是多啦A梦我有肚子!!我是多啦A梦我有肚子!!我是多啦A梦我有肚子!!我是多啦A梦我有肚子!!"];
  10. [user2 setImagePath:@"duolaameng.jpg"];
  11. UserModel *user3 = [[UserModel alloc] init];
  12. [user3 setUsername:@"大雄"];
  13. [user3 setIntroduction:@"我是大雄我谁都怕,我是大雄我谁都怕,我是大雄我谁都怕,我是大雄我谁都怕,我是大雄我谁都怕,我是大雄我谁都怕,"];
  14. [user3 setImagePath:@"daxiong.jpg"];
  15. [tableData addObject:user];
  16. [tableData addObject:user2];
  17. [tableData addObject:user3];
  18. }

还需要在viewDidLoad中调用上面这个方法来创建数据

[objc] view plaincopyprint?
  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4. [self initTableView];
  5. [self createUserData];
  6. }

7、实现TableView的

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

方法为cell赋值

[objc] view plaincopyprint?
  1. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  2. return 1;
  3. }
  4. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  5. return [tableData count];
  6. }
  7. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  8. {
  9. //指定cellIdentifier为自定义的cell
  10. static NSString *CellIdentifier = @"Cell";
  11. //自定义cell类
  12. TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  13. if (cell == nil) {
  14. cell = [[TableViewCell alloc] initWithReuseIdentifier:CellIdentifier];
  15. }
  16. UserModel *user = [tableData objectAtIndex:indexPath.row];
  17. cell.name.text = user.username;
  18. [cell.userImage setImage:[UIImage imageNamed:user.imagePath]];
  19. [cell setIntroductionText:user.introduction];
  20. return cell;
  21. }

8、最后需要将cell的高度返回给

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath方法:

[objc] view plaincopyprint?
  1. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  2. TableViewCell *cell = [self tableView:_tableView cellForRowAtIndexPath:indexPath];
  3. return cell.frame.size.height;
  4. }

这样TableViewController.m中得所有代码:

[objc] view plaincopyprint?
  1. //
  2. //  TableViewController.m
  3. //  AdaptiveCell
  4. //
  5. //  Created by swinglife on 14-1-10.
  6. //  Copyright (c) 2014年 swinglife. All rights reserved.
  7. //
  8. #import "TableViewController.h"
  9. #import "UserModel.h"
  10. #import "TableViewCell.h"
  11. @interface TableViewController (){
  12. NSMutableArray *tableData;  //tableView数据存放数组
  13. }
  14. @end
  15. @implementation TableViewController
  16. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  17. {
  18. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  19. if (self) {
  20. tableData = [[NSMutableArray alloc] init];
  21. }
  22. return self;
  23. }
  24. - (void)viewDidLoad
  25. {
  26. [super viewDidLoad];
  27. [self initTableView];
  28. [self createUserData];
  29. }
  30. //初始化tableView;
  31. -(void)initTableView{
  32. CGRect frame = self.view.frame;
  33. _tableView = [[UITableView alloc] initWithFrame:CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height)];
  34. //代理类
  35. _tableView.delegate = self;
  36. //数据源
  37. _tableView.dataSource = self;
  38. [self.view addSubview:_tableView];
  39. }
  40. //我需要一点测试数据,直接复制老项目东西
  41. -(void)createUserData{
  42. UserModel *user = [[UserModel alloc] init];
  43. [user setUsername:@"胖虎"];
  44. [user setIntroduction:@"我是胖虎我怕谁!!我是胖虎我怕谁!!我是胖虎我怕谁!!"];
  45. [user setImagePath:@"panghu.jpg"];
  46. UserModel *user2 = [[UserModel alloc] init];
  47. [user2 setUsername:@"多啦A梦"];
  48. [user2 setIntroduction:@"我是多啦A梦我有肚子!!我是多啦A梦我有肚子!!我是多啦A梦我有肚子!!我是多啦A梦我有肚子!!我是多啦A梦我有肚子!!我是多啦A梦我有肚子!!我是多啦A梦我有肚子!!我是多啦A梦我有肚子!!"];
  49. [user2 setImagePath:@"duolaameng.jpg"];
  50. UserModel *user3 = [[UserModel alloc] init];
  51. [user3 setUsername:@"大雄"];
  52. [user3 setIntroduction:@"我是大雄我谁都怕,我是大雄我谁都怕,我是大雄我谁都怕,我是大雄我谁都怕,我是大雄我谁都怕,我是大雄我谁都怕,"];
  53. [user3 setImagePath:@"daxiong.jpg"];
  54. [tableData addObject:user];
  55. [tableData addObject:user2];
  56. [tableData addObject:user3];
  57. }
  58. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  59. TableViewCell *cell = [self tableView:_tableView cellForRowAtIndexPath:indexPath];
  60. return cell.frame.size.height;
  61. }
  62. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  63. return 1;
  64. }
  65. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  66. return [tableData count];
  67. }
  68. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  69. {
  70. //指定cellIdentifier为自定义的cell
  71. static NSString *CellIdentifier = @"Cell";
  72. //自定义cell类
  73. TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  74. if (cell == nil) {
  75. cell = [[TableViewCell alloc] initWithReuseIdentifier:CellIdentifier];
  76. }
  77. UserModel *user = [tableData objectAtIndex:indexPath.row];
  78. cell.name.text = user.username;
  79. [cell.userImage setImage:[UIImage imageNamed:user.imagePath]];
  80. [cell setIntroductionText:user.introduction];
  81. return cell;
  82. }
  83. - (void)didReceiveMemoryWarning
  84. {
  85. [super didReceiveMemoryWarning];
  86. }
  87. @end

总结:这种方式是通过计算出UILabel自动换行后的高度后,通过-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 方法将高度返回给TableView然后构建cell的高度。

最后的运行效果:

转载于:https://www.cnblogs.com/yuqingzhude/p/4844898.html

IOS TableView的Cell高度自适应,UILabel自动换行适应相关推荐

  1. barbuttonitem 文字换行_IOS TableView的Cell高度自适应,UILabel自动换行适应

    ios tableView那些事 (八) tableview的插入移动.self.tableview.editinganimated:YES];- (BOOL)tableView:(UITableVi ...

  2. (0072)iOS开发之UITableViewCell高度自适应探索--cell预估高度

    转载自:http://www.jianshu.com/p/f3609cd9392e 有了预估高度这个先决条件,一切都好说了.我们直接从代码入手. 接下来我们实现一个简单的信息展示功能,如: Demo最 ...

  3. SDAutoLayout快速实现Cell的高度自适应

    我们经常会遇到需要cell高度自适应的情况 SDAutoLayout可以帮助你快速的实现这个功能 第一步 cell里面自定义 在这里只显示 姓名 电话 地址 三个控件 -(id)initWithSty ...

  4. iOS UILable高度自适应

    适用于iOS6以后 NSString *tip = @"UILable高度自适应,UILable高度自适应,UILable高度自适应";UILabel label_2 = [[UI ...

  5. 简单的TableViewCell高度自适应(只有Label,仅当参考思路)

    在iOS开发中或多或少的都会碰到TableViewCell高度自适应,那么今天这篇文章就简单的介绍一下如何给tableViewCell自适应高度 #ViewController copy @inter ...

  6. ios html高度自适应,iOS UILabel高度自适应终结篇

    释放双眼,带上耳机,听听看~! 网上大部分的boundingRectWithSize和sizeWithFont 计算出来的宽高在某些有特殊情况下(如链接中有n等等)计算出来的还是有偏差不准,此时用NS ...

  7. IOS开发—IOS7.0以后UILabel高度自适应设置

    IOS7.0以后UILabel高度自适应 IOS7.0以后,UILabel自适应高度的方法发生了改进,以下根据代码展示如何对一个label做自适应文本高度的操作. 代码示例: UILabel *lab ...

  8. 让tableView的高度等于contentSize的高度、动态调整tableView的高度、tableView的高度自适应布局...

    文章概要: 1.简介下,tableView中的内容如何高度自适应的布局 2.如何做到让tableView的高度动态调整 还是看图作文吧- 首先,tableView的高度就是用户能够看见里面更大世界的那 ...

  9. UI一揽子计划 11 (自定义UITableViewCell、Cell 的自适应高度)

    一. 自定义UITableViewCell 在日常的编程中,系统提供的几种Cell 样式 往往不能满足我们的需求.所以需要我们给它进行自定义样式. 自定义Cell 就是创建一个UITableViewC ...

最新文章

  1. linux c 错误 'for' loop initial declaration used outside C99 mode
  2. Codeforces 468C/469E 易错点
  3. POJ 3518 Prime Gap(素数题)
  4. Presto内存管理源码分析
  5. linux 脚本返回值
  6. poj 3032 模拟
  7. TextView的一些高级应用(自定义字体、显示多种颜色、添加阴影)
  8. PreferenceScreen使用
  9. iOS 开发中的各种证书简要说明
  10. mysql hugepage_huge page 能给MySQL 带来性能提升吗?
  11. win10启用smb3多通道_关于win10无法使用smb访问局域网内的计算机
  12. 王禹偁:万壑有声含晚籁,数峰无语立斜阳
  13. 【UE4】获取13位时间戳
  14. 1.5 极限的存在准则和两个重要极限
  15. MVC与MVP的区别
  16. 数据库面试——锁的12连问,赶紧收藏!
  17. [附源码]计算机毕业设计Python的连锁药店销售管理系统(程序+源码+LW文档)
  18. 联想K29昭阳K29笔记本联想K49A在dos下刷入bios教程
  19. IDC FutureScape:2023年中国未来数字创新十大预测
  20. 开启sketchup超速云渲染模式,文末附彩蛋!

热门文章

  1. 找出数组中出现次数超过一半的数
  2. Neuroph studio max net
  3. mahout 算法集
  4. muduo之AsyncLogging
  5. sscanf操作字符串和整型的区别
  6. STM8控制4位LED数码管显示数字
  7. MySQl笔记8:把good表中商品名为‘诺基亚xxxx‘的商品,改为‘HTCxxxx‘
  8. maxcompute 2.0复杂数据类型之struct
  9. python学习中的bug
  10. LoadRunner11设置场景百分比模式完成多台客户端负载测试