IOS TableView的Cell高度自适应,UILabel自动换行适应


项目的源码下载地址:http://download.csdn.net/detail/swingpyzf/6835365


需求:

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

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


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

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

[objc]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  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.   
  9. #import <UIKit/UIKit.h>  
  10.   
  11. @interface TableViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{  
  12.       
  13. }  
  14.   
  15. @property (nonatomic,retainUITableView *tableView;  
  16.   
  17. @end  

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

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


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

[objc]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface TableViewCell : UITableViewCell{  
  4.   
  5. }  
  6.   
  7. //用户名  
  8. @property(nonatomic,retainUILabel *name;  
  9. //用户介绍  
  10. @property(nonatomic,retainUILabel *introduction;  
  11. //用户头像  
  12. @property(nonatomic,retainUIImageView *userImage;  
  13.   
  14. //给用户介绍赋值并且实现自动换行  
  15. -(void)setIntroductionText:(NSString*)text;  
  16. //初始化cell类  
  17. -(id)initWithReuseIdentifier:(NSString*)reuseIdentifier;  
  18. @end  

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

[objc]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  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.   
  9. #import "TableViewCell.h"  
  10.   
  11. @implementation TableViewCell  
  12.   
  13. -(id)initWithReuseIdentifier:(NSString*)reuseIdentifier{  
  14.     self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];  
  15.     if (self) {  
  16.         [self initLayuot];  
  17.     }  
  18.     return self;  
  19. }  
  20. //初始化控件  
  21. -(void)initLayuot{  
  22.     _name = [[UILabel alloc] initWithFrame:CGRectMake(71525040)];  
  23.     [self addSubview:_name];  
  24.     _userImage = [[UIImageView alloc] initWithFrame:CGRectMake(556666)];  
  25.     [self addSubview:_userImage];  
  26.     _introduction = [[UILabel alloc] initWithFrame:CGRectMake(57825040)];  
  27.     [self addSubview:_introduction];  
  28. }  
  29.   
  30. //赋值 and 自动换行,计算出cell的高度  
  31. -(void)setIntroductionText:(NSString*)text{  
  32.     //获得当前cell高度  
  33.     CGRect frame = [self frame];  
  34.     //文本赋值  
  35.     self.introduction.text = text;  
  36.     //设置label的最大行数  
  37.     self.introduction.numberOfLines = 10;  
  38.     CGSize size = CGSizeMake(3001000);  
  39.     CGSize labelSize = [self.introduction.text sizeWithFont:self.introduction.font constrainedToSize:size lineBreakMode:NSLineBreakByClipping];  
  40.     self.introduction.frame = CGRectMake(self.introduction.frame.origin.xself.introduction.frame.origin.y, labelSize.width, labelSize.height);  
  41.       
  42.     //计算出自适应的高度  
  43.     frame.size.height = labelSize.height+100;  
  44.       
  45.     self.frame = frame;  
  46. }  
  47.   
  48. - (void)setSelected:(BOOL)selected animated:(BOOL)animated  
  49. {  
  50.     [super setSelected:selected animated:animated];  
  51.   
  52. }  
  53.   
  54. @end  

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

[objc]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. #import <Foundation/Foundation.h>  
  2.   
  3. @interface UserModel : NSObject  
  4.   
  5. //用户名  
  6. @property (nonatomic,copyNSString *username;  
  7. //介绍  
  8. @property (nonatomic,copyNSString *introduction;  
  9. //头像图片路径  
  10. @property (nonatomic,copyNSString *imagePath;  
  11.   
  12. @end  

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

[objc]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  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.   
  16.       
  17.     [tableData addObject:user];  
  18.     [tableData addObject:user2];  
  19.     [tableData addObject:user3];  
  20. }  

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

[objc]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  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 plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{  
  2.     return 1;  
  3. }  
  4.   
  5. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{  
  6.     return [tableData count];  
  7. }  
  8.   
  9. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  10. {  
  11.     //指定cellIdentifier为自定义的cell  
  12.     static NSString *CellIdentifier = @"Cell";  
  13.     //自定义cell类  
  14.     TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
  15.     if (cell == nil) {  
  16.         cell = [[TableViewCell alloc] initWithReuseIdentifier:CellIdentifier];  
  17.     }  
  18.     UserModel *user = [tableData objectAtIndex:indexPath.row];  
  19.     cell.name.text = user.username;  
  20.     [cell.userImage setImage:[UIImage imageNamed:user.imagePath]];  
  21.     [cell setIntroductionText:user.introduction];  
  22.     return cell;  
  23. }  

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

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

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

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

最后的运行效果:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这里给你一个简单的示例代码,用于演示 UITableView 在数据源发生变化时如何自动更新: ``` // 定义一个数组用于存储数据 NSMutableArray *dataArray = [NSMutableArray arrayWithObjects:@"第一行", @"第二行", @"第三行", nil]; // 实现 UITableView 的数据源方法 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [dataArray count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @"cellIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; } // 获取对应的数据 NSString *data = [dataArray objectAtIndex:indexPath.row]; // 更新 UITableViewCell 的内容 cell.textLabel.text = data; return cell; } // 在某个操作后更新数据源并刷新 UITableView [dataArray removeObjectAtIndex:1]; [tableView reloadData]; ``` 在上面的代码中,我们首先定义了一个数组 `dataArray`,用于存储数据。然后在 UITableView 的数据源方法中,我们根据当前的 indexPath 来获取对应的数据,并使用该数据来更新 UITableViewCell 的内容。 最后,在某个操作(例如删除第二行数据)后,我们需要更新数据源 `dataArray`,并调用 UITableView 的 `reloadData` 方法来刷新 UITableView。这样,UITableView 就会自动更新显示的内容,以反映最新的数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值