日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2025/5/22 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 IOS TableView的Cell高度自适应,UILabel自动换行适应 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

需求:

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

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


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

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

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

  • 2、實現UITableView對象的初始化,聲明一個tableData的數組對象用來保存tableView中得數據

    [objc]?view plaincopyprint?
  • #import?"TableViewController.h"??
  • ??
  • @interface?TableViewController?(){??
  • ????NSMutableArray?*tableData;??//tableView數據存放數組??
  • }??
  • ??
  • @end??
  • ??
  • @implementation?TableViewController??
  • ??
  • -?(id)initWithNibName:(NSString?*)nibNameOrNil?bundle:(NSBundle?*)nibBundleOrNil??
  • {??
  • ????self?=?[super?initWithNibName:nibNameOrNil?bundle:nibBundleOrNil];??
  • ????if?(self)?{??
  • ????????tableData?=?[[NSMutableArray?alloc]?init];??
  • ????}??
  • ????return?self;??
  • }??
  • ??
  • -?(void)viewDidLoad??
  • {??
  • ????[super?viewDidLoad];??
  • ????[self?initTableView];??
  • }??
  • ??
  • //初始化tableView;??
  • -(void)initTableView{??
  • ????CGRect?frame?=?self.view.frame;??
  • ????_tableView?=?[[UITableView?alloc]?initWithFrame:CGRectMake(frame.origin.x,?frame.origin.y,?frame.size.width,?frame.size.height)];??
  • ????//代理類??
  • ????_tableView.delegate?=?self;??
  • ????//數據源??
  • ????_tableView.dataSource?=?self;??
  • ????[self.view?addSubview:_tableView];??
  • }??

  • 3、創建自定義的UITableViewCell類,聲明需要用到的控件對象和方法:

    [objc]?view plaincopyprint?
  • #import?<UIKit/UIKit.h>??
  • ??
  • @interface?TableViewCell?:?UITableViewCell{??
  • ??
  • }??
  • ??
  • //用戶名??
  • @property(nonatomic,retain)?UILabel?*name;??
  • //用戶介紹??
  • @property(nonatomic,retain)?UILabel?*introduction;??
  • //用戶頭像??
  • @property(nonatomic,retain)?UIImageView?*userImage;??
  • ??
  • //給用戶介紹賦值并且實現自動換行??
  • -(void)setIntroductionText:(NSString*)text;??
  • //初始化cell類??
  • -(id)initWithReuseIdentifier:(NSString*)reuseIdentifier;??
  • @end??

  • 4、初始化Cell的用戶控件,實現方法:

    [objc]?view plaincopyprint?
  • //??
  • //??TableViewCell.m??
  • //??AdaptiveCell??
  • //??
  • //??Created?by?swinglife?on?14-1-10.??
  • //??Copyright?(c)?2014年?swinglife.?All?rights?reserved.??
  • //??
  • ??
  • #import?"TableViewCell.h"??
  • ??
  • @implementation?TableViewCell??
  • ??
  • -(id)initWithReuseIdentifier:(NSString*)reuseIdentifier{??
  • ????self?=?[super?initWithStyle:UITableViewCellStyleDefault?reuseIdentifier:reuseIdentifier];??
  • ????if?(self)?{??
  • ????????[self?initLayuot];??
  • ????}??
  • ????return?self;??
  • }??
  • //初始化控件??
  • -(void)initLayuot{??
  • ????_name?=?[[UILabel?alloc]?initWithFrame:CGRectMake(71,?5,?250,?40)];??
  • ????[self?addSubview:_name];??
  • ????_userImage?=?[[UIImageView?alloc]?initWithFrame:CGRectMake(5,?5,?66,?66)];??
  • ????[self?addSubview:_userImage];??
  • ????_introduction?=?[[UILabel?alloc]?initWithFrame:CGRectMake(5,?78,?250,?40)];??
  • ????[self?addSubview:_introduction];??
  • }??
  • ??
  • //賦值?and?自動換行,計算出cell的高度??
  • -(void)setIntroductionText:(NSString*)text{??
  • ????//獲得當前cell高度??
  • ????CGRect?frame?=?[self?frame];??
  • ????//文本賦值??
  • ????self.introduction.text?=?text;??
  • ????//設置label的最大行數??
  • ????self.introduction.numberOfLines?=?10;??
  • ????CGSize?size?=?CGSizeMake(300,?1000);??
  • ????CGSize?labelSize?=?[self.introduction.text?sizeWithFont:self.introduction.font?constrainedToSize:size?lineBreakMode:NSLineBreakByClipping];??
  • ????self.introduction.frame?=?CGRectMake(self.introduction.frame.origin.x,?self.introduction.frame.origin.y,?labelSize.width,?labelSize.height);??
  • ??????
  • ????//計算出自適應的高度??
  • ????frame.size.height?=?labelSize.height+100;??
  • ??????
  • ????self.frame?=?frame;??
  • }??
  • ??
  • -?(void)setSelected:(BOOL)selected?animated:(BOOL)animated??
  • {??
  • ????[super?setSelected:selected?animated:animated];??
  • ??
  • }??
  • ??
  • @end??

  • 5、現在需要一個存放數據對象的模型類,創建一個UserModel用來封裝數據

    [objc]?view plaincopyprint?
  • #import?<Foundation/Foundation.h>??
  • ??
  • @interface?UserModel?:?NSObject??
  • ??
  • //用戶名??
  • @property?(nonatomic,copy)?NSString?*username;??
  • //介紹??
  • @property?(nonatomic,copy)?NSString?*introduction;??
  • //頭像圖片路徑??
  • @property?(nonatomic,copy)?NSString?*imagePath;??
  • ??
  • @end??

  • 6、現在需要一些數據,在TableViewController.m文件中實現數據的創建:

    [objc]?view plaincopyprint?
  • //我需要一點測試數據,直接復制老項目東西??
  • -(void)createUserData{??
  • ????UserModel?*user?=?[[UserModel?alloc]?init];??
  • ????[user?setUsername:@"胖虎"];??
  • ????[user?setIntroduction:@"我是胖虎我怕誰!!我是胖虎我怕誰!!我是胖虎我怕誰!!"];??
  • ????[user?setImagePath:@"panghu.jpg"];??
  • ????UserModel?*user2?=?[[UserModel?alloc]?init];??
  • ????[user2?setUsername:@"多啦A夢"];??
  • ????[user2?setIntroduction:@"我是多啦A夢我有肚子!!我是多啦A夢我有肚子!!我是多啦A夢我有肚子!!我是多啦A夢我有肚子!!我是多啦A夢我有肚子!!我是多啦A夢我有肚子!!我是多啦A夢我有肚子!!我是多啦A夢我有肚子!!"];??
  • ????[user2?setImagePath:@"duolaameng.jpg"];??
  • ????UserModel?*user3?=?[[UserModel?alloc]?init];??
  • ????[user3?setUsername:@"大雄"];??
  • ????[user3?setIntroduction:@"我是大雄我誰都怕,我是大雄我誰都怕,我是大雄我誰都怕,我是大雄我誰都怕,我是大雄我誰都怕,我是大雄我誰都怕,"];??
  • ????[user3?setImagePath:@"daxiong.jpg"];??
  • ??
  • ??????
  • ????[tableData?addObject:user];??
  • ????[tableData?addObject:user2];??
  • ????[tableData?addObject:user3];??
  • }??
  • 還需要在viewDidLoad中調用上面這個方法來創建數據

    [objc]?view plaincopyprint?
  • -?(void)viewDidLoad??
  • {??
  • ????[super?viewDidLoad];??
  • ????[self?initTableView];??
  • ????[self?createUserData];??
  • }??



  • 7、實現TableView的

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

    方法為cell賦值

    [objc]?view plaincopyprint?
  • -(NSInteger)numberOfSectionsInTableView:(UITableView?*)tableView{??
  • ????return?1;??
  • }??
  • ??
  • -(NSInteger)tableView:(UITableView?*)tableView?numberOfRowsInSection:(NSInteger)section{??
  • ????return?[tableData?count];??
  • }??
  • ??
  • -?(UITableViewCell?*)tableView:(UITableView?*)tableView?cellForRowAtIndexPath:(NSIndexPath?*)indexPath??
  • {??
  • ????//指定cellIdentifier為自定義的cell??
  • ????static?NSString?*CellIdentifier?=?@"Cell";??
  • ????//自定義cell類??
  • ????TableViewCell?*cell?=?[tableView?dequeueReusableCellWithIdentifier:CellIdentifier];??
  • ????if?(cell?==?nil)?{??
  • ????????cell?=?[[TableViewCell?alloc]?initWithReuseIdentifier:CellIdentifier];??
  • ????}??
  • ????UserModel?*user?=?[tableData?objectAtIndex:indexPath.row];??
  • ????cell.name.text?=?user.username;??
  • ????[cell.userImage?setImage:[UIImage?imageNamed:user.imagePath]];??
  • ????[cell?setIntroductionText:user.introduction];??
  • ????return?cell;??
  • }??

  • 8、最后需要將cell的高度返回給

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

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

  • 這樣TableViewController.m中得所有代碼:

    [objc]?view plaincopyprint?
  • //??
  • //??TableViewController.m??
  • //??AdaptiveCell??
  • //??
  • //??Created?by?swinglife?on?14-1-10.??
  • //??Copyright?(c)?2014年?swinglife.?All?rights?reserved.??
  • //??
  • ??
  • #import?"TableViewController.h"??
  • #import?"UserModel.h"??
  • #import?"TableViewCell.h"??
  • ??
  • @interface?TableViewController?(){??
  • ????NSMutableArray?*tableData;??//tableView數據存放數組??
  • }??
  • ??
  • @end??
  • ??
  • @implementation?TableViewController??
  • ??
  • -?(id)initWithNibName:(NSString?*)nibNameOrNil?bundle:(NSBundle?*)nibBundleOrNil??
  • {??
  • ????self?=?[super?initWithNibName:nibNameOrNil?bundle:nibBundleOrNil];??
  • ????if?(self)?{??
  • ????????tableData?=?[[NSMutableArray?alloc]?init];??
  • ????}??
  • ????return?self;??
  • }??
  • ??
  • -?(void)viewDidLoad??
  • {??
  • ????[super?viewDidLoad];??
  • ????[self?initTableView];??
  • ????[self?createUserData];??
  • }??
  • ??
  • //初始化tableView;??
  • -(void)initTableView{??
  • ????CGRect?frame?=?self.view.frame;??
  • ????_tableView?=?[[UITableView?alloc]?initWithFrame:CGRectMake(frame.origin.x,?frame.origin.y,?frame.size.width,?frame.size.height)];??
  • ????//代理類??
  • ????_tableView.delegate?=?self;??
  • ????//數據源??
  • ????_tableView.dataSource?=?self;??
  • ????[self.view?addSubview:_tableView];??
  • }??
  • ??
  • //我需要一點測試數據,直接復制老項目東西??
  • -(void)createUserData{??
  • ????UserModel?*user?=?[[UserModel?alloc]?init];??
  • ????[user?setUsername:@"胖虎"];??
  • ????[user?setIntroduction:@"我是胖虎我怕誰!!我是胖虎我怕誰!!我是胖虎我怕誰!!"];??
  • ????[user?setImagePath:@"panghu.jpg"];??
  • ????UserModel?*user2?=?[[UserModel?alloc]?init];??
  • ????[user2?setUsername:@"多啦A夢"];??
  • ????[user2?setIntroduction:@"我是多啦A夢我有肚子!!我是多啦A夢我有肚子!!我是多啦A夢我有肚子!!我是多啦A夢我有肚子!!我是多啦A夢我有肚子!!我是多啦A夢我有肚子!!我是多啦A夢我有肚子!!我是多啦A夢我有肚子!!"];??
  • ????[user2?setImagePath:@"duolaameng.jpg"];??
  • ????UserModel?*user3?=?[[UserModel?alloc]?init];??
  • ????[user3?setUsername:@"大雄"];??
  • ????[user3?setIntroduction:@"我是大雄我誰都怕,我是大雄我誰都怕,我是大雄我誰都怕,我是大雄我誰都怕,我是大雄我誰都怕,我是大雄我誰都怕,"];??
  • ????[user3?setImagePath:@"daxiong.jpg"];??
  • ??
  • ??????
  • ????[tableData?addObject:user];??
  • ????[tableData?addObject:user2];??
  • ????[tableData?addObject:user3];??
  • }??
  • ??
  • -(CGFloat)tableView:(UITableView?*)tableView?heightForRowAtIndexPath:(NSIndexPath?*)indexPath{??
  • ????TableViewCell?*cell?=?[self?tableView:_tableView?cellForRowAtIndexPath:indexPath];??
  • ????return?cell.frame.size.height;??
  • }??
  • ??
  • -(NSInteger)numberOfSectionsInTableView:(UITableView?*)tableView{??
  • ????return?1;??
  • }??
  • ??
  • -(NSInteger)tableView:(UITableView?*)tableView?numberOfRowsInSection:(NSInteger)section{??
  • ????return?[tableData?count];??
  • }??
  • ??
  • -?(UITableViewCell?*)tableView:(UITableView?*)tableView?cellForRowAtIndexPath:(NSIndexPath?*)indexPath??
  • {??
  • ????//指定cellIdentifier為自定義的cell??
  • ????static?NSString?*CellIdentifier?=?@"Cell";??
  • ????//自定義cell類??
  • ????TableViewCell?*cell?=?[tableView?dequeueReusableCellWithIdentifier:CellIdentifier];??
  • ????if?(cell?==?nil)?{??
  • ????????cell?=?[[TableViewCell?alloc]?initWithReuseIdentifier:CellIdentifier];??
  • ????}??
  • ????UserModel?*user?=?[tableData?objectAtIndex:indexPath.row];??
  • ????cell.name.text?=?user.username;??
  • ????[cell.userImage?setImage:[UIImage?imageNamed:user.imagePath]];??
  • ????[cell?setIntroductionText:user.introduction];??
  • ????return?cell;??
  • }??
  • ??
  • -?(void)didReceiveMemoryWarning??
  • {??
  • ????[super?didReceiveMemoryWarning];??
  • }??
  • ??
  • @end??

  • 總結:這種方式是通過計算出UILabel自動換行后的高度后,通過-(CGFloat)tableView:(UITableView?*)tableView heightForRowAtIndexPath:(NSIndexPath?*)indexPath 方法將高度返回給TableView然后構建cell的高度。

    最后的運行效果:

    轉載于:https://www.cnblogs.com/yuqingzhude/p/4844898.html

    總結

    以上是生活随笔為你收集整理的IOS TableView的Cell高度自适应,UILabel自动换行适应的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。