转自:http://www.cnblogs.com/qianLL/p/5393331.html

在github中有许多大牛封装好的第三发类库,其中有个自适应cell高度的类库

下载地址:https://github.com/gsdios/SDAutoLayout

model类

commentsModel

#import "JSONModel.h"
#import "getCommentData.h"@interface commentsModel : JSONModel
@property(nonatomic,copy)NSArray<getCommentData> *commentList;
@end

#import "commentsModel.h"@implementation commentsModel@end

getCommentData

#import "JSONModel.h"@protocol getCommentData@end@interface getCommentData : JSONModel@property(nonatomic,copy)NSString *message;
@property(nonatomic,copy)NSString *nickName;
@property(nonatomic,copy)NSString *createTimeStr;
@end

#import "getCommentData.h"@implementation getCommentData@end

控制器

#import "commentsTableViewController.h"
#import "commentsModel.h"
#import "commentCell.h"
@interface commentsTableViewController ()
@property(nonatomic,strong)NSArray *commentsArray;
@end@implementation commentsTableViewController-(NSArray *)commentsArray{if (_commentsArray==nil) {NSData *data=[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"comment_list.json" ofType:nil]];commentsModel *commensM=[[commentsModel alloc]initWithData:data error:nil];_commentsArray=commensM.commentList;}return _commentsArray;
}- (void)viewDidLoad {[super viewDidLoad];}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}#pragma mark - Table view data source- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {return 1;
}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return self.commentsArray.count;
}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {static NSString *ID=@"comment";commentCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];if (cell==nil) {cell=[[commentCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];}cell.commentData=self.commentsArray[indexPath.row];return cell;
}-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{return [self cellHeightForIndexPath:indexPath cellContentViewWidth:[self cellContentViewWith]];
}-(CGFloat)cellContentViewWith{CGFloat width=[UIScreen mainScreen].bounds.size.width;if ([UIApplication sharedApplication].statusBarOrientation != UIInterfaceOrientationPortrait && [[UIDevice currentDevice].systemVersion floatValue] < 8) {width = [UIScreen mainScreen].bounds.size.height;}return width;
}
@end

具体自定义cell的代码

#import <UIKit/UIKit.h>
@class getCommentData;
@interface commentCell : UITableViewCell
@property(nonatomic,strong)getCommentData *commentData;
@property(nonatomic,strong)UILabel *nameLabel;
@property(nonatomic,strong)UILabel *titleLabel;
@property(nonatomic,strong)UILabel *dateLabel;
@end

#import "commentCell.h"
#import "commentsModel.h"
@implementation commentCell-(void)setCommentData:(getCommentData *)commentData{_commentData=commentData;_titleLabel.text=commentData.message;_dateLabel.text=commentData.createTimeStr;_nameLabel.text=commentData.nickName;}- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];if (self) {[self setup];}return self;
}-(void)setup{_nameLabel=[UILabel new];[self.contentView addSubview:_nameLabel];_nameLabel.textColor=[UIColor colorWithRed:0.891 green:0.549 blue:0.073 alpha:1.000];_nameLabel.font=[UIFont systemFontOfSize:15];_nameLabel.numberOfLines=1;_titleLabel=[UILabel new];[self.contentView addSubview:_titleLabel];_titleLabel.textColor=[UIColor darkGrayColor];_titleLabel.font=[UIFont systemFontOfSize:15];_titleLabel.numberOfLines=0;_dateLabel=[UILabel new];[self.contentView addSubview:_dateLabel];_dateLabel.textColor=[UIColor colorWithRed:0.679 green:0.166 blue:0.828 alpha:1.000];_dateLabel.font=[UIFont systemFontOfSize:15];_dateLabel.numberOfLines=1;CGFloat margin=10;UIView *contentView=self.contentView;_nameLabel.sd_layout.leftSpaceToView(contentView,margin).topSpaceToView(contentView,margin).rightSpaceToView(contentView,margin).heightIs(20);_titleLabel.sd_layout.leftSpaceToView(contentView,margin).topSpaceToView(_nameLabel,2).rightSpaceToView(contentView,margin).autoHeightRatio(0);_dateLabel.sd_layout.leftSpaceToView(contentView,margin).topSpaceToView(_titleLabel,5).heightIs(20).widthIs(150);[self setupAutoHeightWithBottomViewsArray:@[_titleLabel,_dateLabel,_nameLabel] bottomMargin:margin];
}- (void)awakeFromNib {
}- (void)setSelected:(BOOL)selected animated:(BOOL)animated {[super setSelected:selected animated:animated];}@end

转载于:https://www.cnblogs.com/wujie123/p/5397210.html

iOS tableViewCell自适应高度 第三发类库相关推荐

  1. IOS UITextView自适应高度

    LOFTER app需要实现了一个类似iPhone短信输入框的功能,它的功能其实蛮简单,就是:[UITextView的高度随着内容高度的变化而变化].实现思路应该是: 在UITextView的text ...

  2. [ios]UITableViewCell自适应高度 【转】

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {     ...

  3. ios webview自适应高度及关闭回弹效果

    /////////////////////////////初始化,self.view是父控件///////////////////////////////// _webView = [[UIWebVi ...

  4. IOS UILabel 根据内容自适应高度

    iOS Label 自适应高度  适配iOS7以后的版本 更多 self.contentLabelView = [[UILabel alloc] init]; self.contentLabelVie ...

  5. ios wkweb设置图片_iOS 之 WKWebView自适应高度获取网络图片

    WKWebView是iOS8中引入的新组建,苹果将 UIWebViewDelegate 与 UIWebView 重构成了14个类和3个协议并引入了不少新的功能和接口,它代替了UIKit 中的 UIWe ...

  6. ios 获取html的高度,iOS Webview自适应实际内容高度的4种方法详解

    //第一种方法 - (void)webViewDidFinishLoad:(UIWebView *)webView { CGFloat webViewHeight=[webView.scrollVie ...

  7. iOS即时通讯输入框随字数自适应高度

    代码地址如下: http://www.demodashi.com/demo/13210.html 前言 本人最近在研究socket与聊天界面的UI,在写聊天界面UI的时候是模仿微信的界面其中的文字输入 ...

  8. IOS Swift语言开发 tableView的重用以及自cell的自适应高度

    http://www.aichengxu.com/iOS/11143168.htm 一.准备数据 (这是一个元组,第一个元素为英雄的名字;第二个元素为英雄头像图片的名字,格式为.PNG,如果为其他的格 ...

  9. iOS中AutoLayout自动布局,自适应高度

    以往我们做cell的自适应的时候都是要写许多的代码进行计算高度,而且在适配的时候容易出现问题,费时耗工.那么下面我们就共同探讨一种基于xib的自动计算高度的方法 1.我们先创建tableView,ta ...

最新文章

  1. 浅谈死链接和错误链接,如何防范死链接发生
  2. jsp里面的input的值吗_一个jsp页面中的input框向另一个jsp页面的input框传值
  3. 怎么样快速学习AngularJS?
  4. Plsql运行mysql脚本_oracle中PLSQL语句
  5. string、stringbuilder、stringbuffer区别
  6. c语言求一个数的阶乘值代码,求10000的阶乘(c语言代码实现)
  7. SELECTION-SCREEN 加按钮
  8. KMP算法 next数组 nextval数组
  9. MyEclipse 中各种 libraries 的含义
  10. 2009程序员考试大纲
  11. 变色龙配置文件功能介绍
  12. codeforces 1384A(构造)
  13. 如何在BIOS中设置RAID?
  14. java-->if顺序结构-->骰子游戏(小案例)
  15. 2017年全国研究生电子设计大赛上海赛区感触
  16. GC8418 数字光纤音频解码芯片 光纤解码芯片 MS8412替代
  17. 你该选择哪种编程语言来开发App呢?
  18. 底什么是伪静态?为什么要做伪静态?
  19. Oracle 查询技巧与优化(一) 单表查询与排序
  20. Android获取GPS网络定位经纬度信息

热门文章

  1. 强人总结的哄老婆秘籍
  2. 微软的日历控件为什么从1753年开始?Sqlserver数据库不能插入1753年之前的数据?...
  3. ADAS辅助驾驶_自动驾驶_技术点列表
  4. CSS滤镜(Filters)
  5. 用py2exe打包后的程序一闪而过
  6. 3.22 爬虫小记
  7. 数据库之存储引擎,数据类型-30
  8. Matlab调用Java类访问数据库
  9. 知乎上8个100K+高赞回答(筛选自63万个回答)
  10. 蔡先生论道大数据十九:王羲之与大数据