在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 ;
} - (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] < ) {
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:];
_nameLabel.numberOfLines=; _titleLabel=[UILabel new];
[self.contentView addSubview:_titleLabel];
_titleLabel.textColor=[UIColor darkGrayColor];
_titleLabel.font=[UIFont systemFontOfSize:];
_titleLabel.numberOfLines=; _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:];
_dateLabel.numberOfLines=; CGFloat margin=;
UIView *contentView=self.contentView; _nameLabel.sd_layout
.leftSpaceToView(contentView,margin)
.topSpaceToView(contentView,margin)
.rightSpaceToView(contentView,margin)
.heightIs(); _titleLabel.sd_layout
.leftSpaceToView(contentView,margin)
.topSpaceToView(_nameLabel,)
.rightSpaceToView(contentView,margin)
.autoHeightRatio(); _dateLabel.sd_layout
.leftSpaceToView(contentView,margin)
.topSpaceToView(_titleLabel,)
.heightIs()
.widthIs();
[self setupAutoHeightWithBottomViewsArray:@[_titleLabel,_dateLabel,_nameLabel] bottomMargin:margin];
} - (void)awakeFromNib {
} - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated]; } @end

iOS tableViewCell自适应高度 第三发类库的更多相关文章

  1. 【原】ios tableViewCell 自适应高度

    原文:http://www.cnblogs.com/A--G/p/4819051.html 前言:之前在做一个类似微博的小需求时候,用table view实现了微博文字和图片等等的基本展示,由于文字和 ...

  2. IOS UITextView自适应高度

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

  3. iOS Label 自适应高度

    推荐第二个 测试一,只改变numberOfLines属性,label的高度不会自适应(会有text中的一部分内容称为......) NSString *str = @"jgreijgirje ...

  4. iOS 学习 - 8 TableViewCell 自适应高度

    思路:计算文字的高度,存进数组 加注:存在中文,需要加一行文字的高度,也就是 font 主要代码 #pragma mark -- UITableViewDelegate - (CGFloat)tabl ...

  5. iOS UITableViewableViewCell自适应高度

    前两天做了一个项目,中间有遇到一个问题,就是聊天的时候cell高度的问题.这是一个很多前辈都遇到过,并且很完美的解决过的问题.这里主要是记录自己的学习心得.项目中首先想到的是用三方库,可是有问题,遂放 ...

  6. iOS UITextView自适应高度UITextContainerView抖动问题

    在打造一个类似于微信朋友圈评论输入框的时候,需要动态调整输入框的高度, 但是,在调整了UITextView的高度之后,继续输入会导致内容(UITextContainerView里的文字)抖动. scr ...

  7. TableViewCell自适应高度

    //初始化TableView时设置 self.tv.estimatedRowHeight=54;self.tv.rowHeight=UITableViewAutomaticDimension;

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

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

  9. iOS 【终极方案】精准获取webView内容高度,自适应高度

    前言:是这样的,刚写完上一篇文章还没缓过神来,上一篇文章我还提到了,想和大家聊聊原生+H5如何无缝连接的故事.结果我朋友就给我发了两篇他的作品.他的做法也都有独到之处.好的文章都是这样,让你每次看都能 ...

随机推荐

  1. Web APi入门之移除XML格式(一)

    前言 回头想来,没想到自己却坚持下来了,EntityFramework系列终于全部完成了,给自己点个赞先.本系列将着手于Web API,关于一些基础的介绍及定义就不再叙述,请参考园友们文章,非常详细, ...

  2. MySQL学习笔记五:数据类型

    MySQL支持多种数据类型,大致可以分为数值,日期/时间和字符类型. 数值类型 MySQL支持所有标准SQL数值数据类型,包括严格数值数据类型(INTEGER.SMALLINT.DECIMAL和NUM ...

  3. linux标准IO缓冲(apue)

    为什么需要标准IO缓冲? LINUX用缓冲的地方遍地可见,不管是硬件.内核还是应用程序,内核里有页高速缓冲,内存高速缓冲,硬件更不用说的L1,L2 cache,应用程序更是多的数不清,基本写的好的软件 ...

  4. jdk8中java.util.concurrent包分析

    并发框架分类 1. Executor相关类 Interfaces. Executor is a simple standardized interface for defining custom th ...

  5. IE 11 如何设置“用户代理字符串”

    "用户代理字符串"这个术语在IE11 之前被叫作“浏览器模式”(Browser Mode),在IE11下面这个特殊的选项被放在F12的“仿真”选项下面了. Figure 1通过按下 ...

  6. 表空间基于时间点的恢复(TSPITR)

    环境:RHEL 6.4 + Oracle 11.2.0.4 准备模拟环境 1. 验证表空间的依赖性 2. 确定执行TSPITR后会丢失的对象 3. 自动执行TSPITR Reference 准备模拟环 ...

  7. Solr学习总结(六)SolrNet的高级用法(复杂查询,分页,高亮,Facet查询)

    上一篇,讲到了SolrNet的基本用法及CURD,这个算是SolrNet 的入门知识介绍吧,昨天写完之后,有朋友评论说,这些感觉都被写烂了.没错,这些基本的用法,在网上百度,资料肯定一大堆,有一些写的 ...

  8. VS2013预览版安装 体验截图

    支持与msdn帐号链接: 不一样的团队管理: 新建项目:

  9. [Java 安全]消息摘要与数字签名

    消息摘要 算法简述 定义 它是一个唯一对应一个消息或文本的固定长度的值,它由一个单向Hash加密函数对消息进行作用而产生.如果消息在途中改变了,则接收者通过对收到消息的新产生的摘要与原摘要比较,就可知 ...

  10. [WCF编程]10.操作:单向操作

    一.单向操作概述 WCF提供了单向操作,一旦客户端调用,WCF会生成一个请求,但没有相关的应答信息返回给客户端.所以,单向操作是不能有返回值,服务抛出的任何异常都不会传递给客户端. 理想情况下,一旦客 ...