IOS TableView的Cell高度自適應,UILabel自動換行適應

項目的源碼下載地址:http://download.csdn.net/detail/swingpyzf/6835365


需求:

1、表格裏的UILable要求自動換行

2、創建的tableViewCell的高度會自動適應內容的高度


一、用xcode構建項目,創建一個有tableView的視圖,用純代碼的形式實現:

1、創建一個UIViewController類,定義一個UITableView,實現TableView的委託和數據源協議

  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中得數據

  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類,聲明需要用到的控件對象和方法:

  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的用戶控件,實現方法:

  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用來封裝數據

  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文件中實現數據的創建:

  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中調用上面這個方法來創建數據

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



7、實現TableView的

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

方法爲cell賦值

  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方法:

  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中得所有代碼:

  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的高度。

最後的運行效果:


發佈了19 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章