一、用xcode构建项目,创建一个有tableView的视图,用纯代码的形式实现:
1、创建一个UIViewController类,定义一个UITableView,实现TableView的委托和数据源协议
#import <UIKit/UIKit.h> @interface TableViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{ } @property (nonatomic,retain) UITableView *tableView; @end
实现UITableView对象的初始化,声明一个tableData的数组对象用来保存tableView中得数据
#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]; }
创建自定义的UITableViewCell类,声明需要用到的控件对象和方法:
#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
初始化Cell的用户控件,实现方法:
#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
相关推荐
swift ,使用autolayout + storyboard 最外层tableview列表,cell里面嵌套了tableview,自适应内容,cell里的tableview不可滑动,内容全部显示,且文字分行显示,不用设置cell的高度直接可以自适应内容
弄着玩玩,大神勿喷https://github.com/RockChanel/SelwynFormDemo
tableview手动适应cell高度,以及iOS8之后自动适应cell高度
cell布局使用SDAutoLayout约束 简单方便快捷
tableviewcell 输入内容实时刷新高度 自适应高度 有不足的地方请指教 分享一些互相学习
支持Cell和Tableview高度自适应,Label和ScrollView内容自适应,致力于做最简单易用的AutoLayout库。 (此图示例详见demo7) 只需要3部设置就可以实现多cell的tableview高度自适应。 用到的库:...
通过estimatedHeightForRowAtIndexPath: Masonry ,自动适配cell
应用ios8新特性,ios8下tableviewcell内容自适应
tableView嵌套多个webView自适应高度,高度获取较精准,速度快,有需要的童鞋可以借鉴
巧妙运用 自动布局动态计算cell 高度
利用纯代码实现,两种最容易实现自适应高度的方法。一看就能学会。
swift tableview cell展开/折叠 填写信息, tableview自适应高度,tableview的cell嵌套tableview 自适应
WKWebView调用js 代码自适应cell高度 github下载地址:https://github.com/yanhaiqiang/HQWebViewFit.git
在简书上看的文章,上面用swift写的自适应,模仿写了OC的tableview自适应,省去了繁琐的对控件计算大小。
博客中的IOS TableView的Cell高度自适应,UILabel自动换行适应 的项目源码
支持Cell和Tableview高度自适应,Label和ScrollView内容自适应,致力于做最简单易用的AutoLayout库。) 图示demo为 “SDAutoLayout实现的对scrollview多种形式的自动布局以及contentsize自适应(带动画效果)” ...
TableVIew使用MMVC布局 ,cell点击展开高度自适应
swift 3 tableview cell 自适应文字展开动画
tableview高度自适应,主要是用来计算文字高度的,结构简单易懂
tableView滑动导航栏消失,和自适应,提示框可以看看瞧瞧,给给意见啊