详解iOS tableViewCell自适应高度 第三发类库

 更新时间:2016年04月15日 11:02:13   作者:徒步天涯  
在github中有许多大牛封装好的第三发类库,其中有个自适应cell高度的类库。接下来通过本文给大家介绍iOS tableViewCell自适应高度 第三发类库,需要的朋友参考下

在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 

相关文章

  • iOS实现代码只执行一次

    iOS实现代码只执行一次

    本文给大家分享的是在iOS中控制代码在整个软件生命周期中只运行一次的代码,有需要的小伙伴可以参考下。
    2016-03-03
  • iOS应用设计模式开发中对简单工厂和工厂方法模式的运用

    iOS应用设计模式开发中对简单工厂和工厂方法模式的运用

    这篇文章主要介绍了iOS应用设计模式开发中对简单工厂和工厂方法模式的运用,示例代码为传统的Objective-C,需要的朋友可以参考下
    2016-03-03
  • iOS中各种UI控件属性设置示例代码

    iOS中各种UI控件属性设置示例代码

    这篇文章主要给大家介绍了关于iOS中各种UI控件属性设置的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-08-08
  • 如何在iOS上使用MVVM进行路由详解

    如何在iOS上使用MVVM进行路由详解

    这篇文章主要给大家介绍了关于如何在iOS上使用MVVM进行路由的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-01-01
  • iOS开发实现图片浏览功能

    iOS开发实现图片浏览功能

    这篇文章主要为大家详细介绍了iOS开发实现图片浏览功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01
  • iOS开发中使用Picker View实现一个点菜应用的UI示例

    iOS开发中使用Picker View实现一个点菜应用的UI示例

    这篇文章主要介绍了iOS开发中使用Picker View实现一个点菜应用的UI示例,代码基于传统的Objective-C,需要的朋友可以参考下
    2016-01-01
  • iOS App开发中通过UIDevice类获取设备信息的方法

    iOS App开发中通过UIDevice类获取设备信息的方法

    UIDevice最常见的用法就是用来监测iOS设备的电量了,然后再实现电池状态通知非常方便,除此之外还有传感器等信息的获取,这里我们就来总结一下iOS App开发中通过UIDevice类获取设备信息的方法:
    2016-07-07
  • 苹果公司推出的新编程语言Swift简介和入门教程

    苹果公司推出的新编程语言Swift简介和入门教程

    这篇文章主要介绍了苹果公司推出的新编程语言Swift简介和入门教程,Swift是苹果于WWDC 2014.6.3发布的编程语言,主要用来替代Objective-C,需要的朋友可以参考下
    2014-06-06
  • iOS开发--仿新闻首页效果WMPageController的使用详解

    iOS开发--仿新闻首页效果WMPageController的使用详解

    这篇文章主要介绍了iOS开发--仿新闻首页效果WMPageController的使用详解,详解的介绍了iOS开发中第三方库WMPageController控件的使用方法,有需要的可以了解下。
    2016-11-11
  • 讨论在线教室 iOS 端声音问题综合解决方案

    讨论在线教室 iOS 端声音问题综合解决方案

    这篇文章主要介绍了在线教室 IOS 端声音问题综合解决方案,从背景介绍,到技术总结,再到行业现状,都进行了细致的说明,建议同学们仔细看一下
    2021-04-04

最新评论