zoukankan      html  css  js  c++  java
  • IOS TableView的Cell高度自适应,UILabel自动换行适应

    需求:

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

    最后的运行效果:

  • 相关阅读:
    SQL Server 2005 中的分区表和索引 [轉]
    [导入]使用RDLC报表(一)
    正则表达式30分钟入门教程[轉]
    C#的TextBox控件输入测试只允许输入数字的测试:
    c#创建access数据库和数据表[转]
    [导入]使用RDLC报表(二)使用自定义数据集
    ASP.net 文件下載
    [导入]使用RDLC报表(四)钻取式报表
    [导入]使用RDLC报表(三)向RDLC报表传入参数
    C# SQL server2000中保存的图像
  • 原文地址:https://www.cnblogs.com/yuqingzhude/p/4844898.html
Copyright © 2011-2022 走看看