tableviewcell 自适应高度(模型数据自适应)
来源:互联网 发布:美国非农就业数据9月 编辑:程序博客网 时间:2024/04/29 05:59
首先先来理一下具体步骤:
1、新建一个自定义的tableviewcell类
2、初始化tableviewcell时同时初始化所有子控件<这里可以先设置子控件的一些属性包括字体、颜色等>
3、提供两个模型类
a、一个是我们常用的数据模型
b、一个是frame模型(数据模型+所有子控件的frame+cell的高度)起个名modelFrame
4、在自定义的cell 中应该提供一个frame属性<modelFrame>
a、将modelFrame模型传递给cell
b、cell根据modelFrame模型给子控件设置frame,根据数据模型给子控件设置具体的数据
5、在tableview的数据源方法中返回cell 的高度
上代码
1、新建一个自定义的tableviewcell类
#import <UIKit/UIKit.h>
@class DescriptionModelFrame;
@interface DetailCell :UITableViewCell
@property(nonatomic,strong)DescriptionModelFrame * DesFrame;
@end
实现文件中#import "DetailCell.h"
#import "DescriptionModel.h"
#import "DescriptionModelFrame.h"
@interface DetailCell()
@property(nonatomic,strong)UILabel * titleLabel;标题
@property(nonatomic,strong)UILabel * desLabel;详细描述
@end
@implementation DetailCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [superinitWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.selectionStyle =UITableViewCellSelectionStyleNone;//去掉cell 点击效果
[selfinitSubControl];
}
return self;
}
-(void)initSubControl
{
UILabel * titleLable = [[UILabelalloc] init];
titleLable.font = [UIFontsystemFontOfSize:15*ScreenZoomScaleSix];
titleLable.backgroundColor = [UIColorwhiteColor];
self.titleLabel = titleLable;
[self.contentViewaddSubview:titleLable];
UILabel * desLabel = [[UILabelalloc] init];
desLabel.font = [UIFontsystemFontOfSize:15*ScreenZoomScaleSix];
desLabel.numberOfLines =0;
desLabel.textColor = [UIColorlightGrayColor];
desLabel.backgroundColor = [UIColorwhiteColor];
self.desLabel = desLabel;
[self.contentViewaddSubview:desLabel];
}
-(void)setDesFrame:(DescriptionModelFrame *)DesFrame
{
_DesFrame = DesFrame;
DescriptionModel * model = DesFrame.desModel;
self.titleLabel.frame = DesFrame.titleLabelF;
self.titleLabel.text = model.attrName;
self.desLabel.frame = DesFrame.desLabelF;
self.desLabel.text = model.attrValue;
}
@end
3、提供两个模型类
a、一个是我们常用的数据模型
#import <Foundation/Foundation.h>
@interface DescriptionModel :NSObject
/**
* 标题
*/
@property(nonatomic,strong)NSString * attrName;
/**
* 详细描述
*/
@property(nonatomic,strong)NSString * attrValue;
@end
b、一个是frame模型(数据模型+所有子控件的frame+cell的高度)起个名modelFrame#import <Foundation/Foundation.h>
@class DescriptionModel;
@interface DescriptionModelFrame :NSObject
@property(nonatomic,assign)CGRect titleLabelF; 标题frame
@property(nonatomic,assign)CGRect desLabelF; 详情frame
@property(nonatomic,strong)DescriptionModel * desModel; 数据模型
@property(nonatomic,assign)CGFloat cellH; 单元格的高度
@end
实现文件中#import "DescriptionModelFrame.h"
#import "DescriptionModel.h"
#define SCREENW [UIScreen mainScreen].bounds.size.width
#define LEFTMEGIN 10
@implementation DescriptionModelFrame
-(void)setDesModel:(DescriptionModel *)desModel
{
_desModel = desModel;
CGFloat cellW = SCREENW;
CGFloat titleW = cellW-2*10*ScreenZoomScaleSix;
CGFloat titleH =15*ScreenZoomScaleSix;
CGFloat titleX =LEFTMEGIN*ScreenZoomScaleSix;
CGFloat titleY =10*ScreenZoomScaleSix;
self.titleLabelF =CGRectMakeLiu(titleX, titleY, titleW, titleH);
CGSize desSize = [selfsizeWithString:desModel.attrValuefont:[UIFontsystemFontOfSize:15*ScreenZoomScaleSix]];
CGFloat desX =LEFTMEGIN*ScreenZoomScaleSix;
CGFloat desY =CGRectGetMaxY(self.titleLabelF)+LEFTMEGIN*ScreenZoomScaleSix;
self.desLabelF = (CGRect){{desX,desY},desSize};
CGFloat cellH =CGRectGetMaxY(self.desLabelF)+LEFTMEGIN*ScreenZoomScaleSix;
self.cellH = cellH;
}
- (CGSize)sizeWithString:(NSString *)string font:(UIFont *)font
{
CGRect rect = [stringboundingRectWithSize:CGSizeMake(SCREENW-10*ScreenZoomScaleSix*2,8000) options:NSStringDrawingTruncatesLastVisibleLine |NSStringDrawingUsesFontLeading |NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName: font} context:nil];
return rect.size;
}
@end
NSArray * modeleArray = [DescriptionModelobjectArrayWithKeyValuesArray:self.dataDic[@"goodsDetail"][@"goodsAttrs"]];
NSArray * modelFrameArray = [selfmodelsFramesWithModls:modeleArray];
self.modelFrameArray = [modelFrameArraymutableCopy];
//数据模型转frame模型
- (NSArray *)modelsFramesWithModls:(NSArray *)models
{
NSMutableArray *frames = [NSMutableArrayarray];
for (DescriptionModel *modelin models) {
DescriptionModelFrame *f = [[DescriptionModelFramealloc] init];
f.desModel = model;
[framesaddObject:f];
}
return frames;
}
// 给cell传递模型数据
cell.DesFrame =self.modelFrameArray[indexPath.row];
cell的高度也出来了
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
DescriptionModelFrame *frame =self.modelFrameArray[indexPath.row];
return frame.cellH;
}
给个图参考一下 我把单元格分割线去掉了当然如果搞复杂的单元格 思路是对的 只不过 计算的东西多了
- tableviewcell 自适应高度(模型数据自适应)
- tableViewCell 自适应高度
- 自适应tableViewCell的高度
- tableViewcell高度自适应
- tableViewcell高度自适应
- TableViewCell自适应高度
- tableViewCell自适应高度
- TableViewCell高度的自适应
- [iOS] TableViewCell 自适应高度
- TableviewCell高度自适应
- tableViewCell 上面webView 自适应高度
- iOS tableviewcell 高度自适应方法
- TableViewCell自适应高度(异步下载)
- IOS-根据TableviewCell 总体高度 做自适应
- tableViewCell根据内容自适应高度的方法
- TableViewCell高度自适应的实现方案
- tableViewCell自适应高度(QQ聊天室模拟)
- IOS TableViewCell嵌套webview 自适应高度
- Picking, Registration, Reservation, Marking..What’s the Difference?
- 一个小白看Android与Java之间的关系
- leetcode之Evaluate Reverse Polish Notation
- java向数据库中插入数据,数据库乱码问题解决
- gson数据格式转化为对象或者集合等
- tableviewcell 自适应高度(模型数据自适应)
- C#中的Dictionary简介
- 检测App的内存占用和泄漏
- Leptonica在VS2010中的编译二,编译 Leptonica1.68
- jQuery简介及语法
- unity3d shader中RenderType的所有类型
- left join on and 与 left join on where的区别
- Java环境的搭建
- 学习网站/博客论坛推荐(三)