//——---------------
当cell的高度不一致时使用
----------
1>新建一个继承自UITableViewCell的类
2>重写initWithStyle:reuseIdentifier:方法
Ø 添加所有需要显示的子控件(不需要设置子控件的数据和frame, 子控件要添加到contentView中)
Ø 进行子控件一次性的属性设置(有些属性只需要设置一次, 比如字体\固定的图片)
3>提供2个模型
Ø 数据模型: 存放文字数据\图片数据
Ø frame模型: 存放数据模型\所有子控件的frame\cell的高度
4>cell拥有一个frame模型(不要直接拥有数据模型)
5>重写frame模型属性的setter方法: 在这个方法中设置子控件的显示数据和frame
6>frame模型数据的初始化已经采取懒加载的方式(每一个cell对应的frame模型数据只加载一次)
|
//——---------------------————
———————数据
模型
文件.h
----------------------------------------
数据模型.h
@interface Model :
NSObject
@property (
nonatomic,
copy)
NSString *text;
//
内容
@property ( nonatomic, copy) NSString *icon; // 头像 @property ( nonatomic, copy) NSString *name; // 昵称 @property ( nonatomic, copy) NSString *picture; // 配图 @property ( nonatomic, assign) BOOL vip;
- (
instancetype)initWithDict:(
NSDictionary *)dict;
+ (
instancetype)modelWithDict:(
NSDictionary *)dict;
@end
|
//——---------------------————
———————数据
模型
文件.m
----------------------------------------
数据模型.m
#import
“
dataModel.h
"
@implementation dataModel
- ( instancetype)initWithDict:( NSDictionary *)dict { if ( self = [ super init]) { [ self setValuesForKeysWithDictionary:dict]; } return self; }
+ (
instancetype)modelWithDict:(
NSDictionary *)dict
{
return [[ self alloc] initWithDict:dict]; }
@end
|
//——---------------------————
———————frame
模型
文件.h
----------------------------------------
frame模型.h
#import
<Foundation/Foundation.h>
@class
dataModel;
@interface frameModel :
NSObject
/**
* 头像的 frame */ @property ( nonatomic, assign, readonly) CGRect iconF; /** * 昵称的 frame */ @property ( nonatomic, assign, readonly) CGRect nameF; /** * 会员图标的 frame */ @property ( nonatomic, assign, readonly) CGRect vipF; /** * 正文的 frame */ @property ( nonatomic, assign, readonly) CGRect textF; /** * 配图的 frame */ @property ( nonatomic, assign, readonly) CGRect pictureF; /** * cell 的高度 */ @property ( nonatomic, assign, readonly) CGFloat cellHeight;
@property (
nonatomic,
strong)
dataModel *model;
@end
|
//——---------------------————
———————frame
模型
文件.m
----------------------------------------
frame模型.m
#import
"frameModel.h"
#import "dataModel.h" #define textFont [UIFont systemFontOfSize: 15 ] #define nameFont [UIFont systemFontOfSize: 17 ] @implementation frameModel - ( void)setModel:( dataModel *)model { _model = model; //1. 设置头像 frame CGFloat iconX = 10; CGFloat iconY = 10; CGFloat iconW = 40; CGFloat iconH = 40; _iconF = CGRectMake(iconX, iconY, iconW, iconH); //2. 设置昵称 CGSize nameSize = [ self sizeWithText: self. model. name font: nameFont maxSize: CGSizeMake( MAXFLOAT, MAXFLOAT)]; CGFloat nameX = CGRectGetMaxX( _iconF) + iconX; CGFloat nameY = iconY + (iconH - nameSize. height) * 0.5; _nameF = CGRectMake(nameX, nameY, nameSize. width, nameSize. height); //3. 设置 vip CGFloat vipX = iconX * 3+ iconW + nameSize. width; CGFloat vipY = nameY; CGFloat vipW = 15; CGFloat vipH = 15; _vipF = CGRectMake(vipX, vipY, vipW, vipH); //4. 设置 text CGSize textSize = [ self sizeWithText: self. model. text font: textFont maxSize: CGSizeMake( 300, MAXFLOAT)]; CGFloat textX = iconX; CGFloat textY = iconY + iconH + iconX; _textF = CGRectMake(textX, textY, textSize. width, textSize. height); //5. 设置配图 CGFloat padding = 10; if ( self. model. picture) { // 有配图 CGFloat pictureX = textX; CGFloat pictureY = CGRectGetMaxY( _textF) + padding; CGFloat pictureW = 100; CGFloat pictureH = 100; _pictrueF = CGRectMake(pictureX, pictureY, pictureW, pictureH); _cellHight = CGRectGetMaxY( _pictrueF) + padding; } else { _cellHight = CGRectGetMaxY( _textF) + padding; } } - ( CGSize)sizeWithText:( NSString *)text font:( UIFont *)font maxSize:( CGSize)maxSize { NSDictionary *attrs = @{ NSFontAttributeName : font }; return [text boundingRectWithSize:maxSize options: NSStringDrawingUsesLineFragmentOrigin attributes:attrs context: nil]. size;
}
|
//——---------------------————
———————View
文件.h
----------------------------------------
cell.h 继承于
UITableViewCell
#import
<UIKit/UIKit.h>
#import "frameModel.h" @interface MyTableViewCell : UITableViewCell @property ( nonatomic, strong) frameModel *frameModel; + ( instancetype)cellWithTableView:( UITableView *)tableView;
@end
|
//——---------------------————
———————View
文件.m
----------------------------------------
cell.m
#import "MyTableViewCell.h" #import "frameModel.h" #import "dataModel.h" #define textFont [UIFont systemFontOfSize: 15 ] #define nameFont [UIFont systemFontOfSize: 17 ] @interface MyTableViewCell() @property ( nonatomic, weak) UIImageView *iconImage; @property ( nonatomic, weak) UILabel *text; @property ( nonatomic, weak) UILabel *name; @property ( nonatomic, weak) UIImageView *vipImage; @property ( nonatomic, weak) UIImageView *pictureImage; @end @implementation MyTableViewCell //1. 创建 UI - ( instancetype)initWithStyle:( UITableViewCellStyle)style reuseIdentifier:( NSString *)reuseIdentifier { if ( self = [ super initWithStyle:style reuseIdentifier:reuseIdentifier]) { //1. 头像 UIImageView *iconImage = [[ UIImageView alloc] init]; [ self. contentView addSubview:iconImage]; self. iconImage = iconImage; //2. 设置文本 UILabel *text = [[ UILabel alloc] init]; text. font = textFont; text. numberOfLines = 0; [ self. contentView addSubview:text]; self. text = text; //3. 设置昵称 UILabel *name = [[ UILabel alloc] init]; name. font = nameFont; name. numberOfLines = 0; [ self. contentView addSubview:name]; self. name = name; //4. 设置 vip UIImageView *vipImage = [[ UIImageView alloc] init]; vipImage. image = [ UIImage imageNamed: @"vip"]; [ self. contentView addSubview:vipImage]; self. vipImage = vipImage; //5. 设置配图 UIImageView *pictureImage = [[ UIImageView alloc] init]; [ self. contentView addSubview:pictureImage]; self. pictureImage = pictureImage; } return self; } //2. 设置数据和 frame - ( void)setFrameModel:( frameModel *)frameModel { _frameModel = frameModel; // 设置数据 [ self setData]; // 设置 frame [ self setFrame]; } - ( void)setData { dataModel *model = self. frameModel. model; //1. 设置头像数据 self. iconImage. image = [ UIImage imageNamed:model. icon]; //2. 设置昵称 self. name. text = model. name; //3. 设置 vip if (model. vip) { self. vipImage. hidden = NO; self. name. textColor = [ UIColor redColor]; } else { self. vipImage. hidden = YES; self. name. textColor = [ UIColor blackColor]; } //4. 设置文本 self. text. text = model. text; //5. 设置配图 if (model. picture) { self. pictureImage. image = [ UIImage imageNamed:model. picture]; self. pictureImage. hidden = NO; } else { self. pictureImage. hidden = YES; } } - ( void)setFrame { //1. 设置头像 frame self. iconImage. frame = self. frameModel. iconF; //2. 设置昵称 self. name. frame = self. frameModel. nameF; //3. 设置 vip self. vipImage. frame = self. frameModel. vipF; //4. 设置 text self. text. frame = self. frameModel. textF; //5. 设置配图 self. pictureImage. frame = self. frameModel. pictrueF; } + ( instancetype)cellWithTableView:( UITableView *)tableView { static NSString *ID = @"status"; MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (cell == nil) { cell = [[ MyTableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:ID]; } return cell; }
@end
|
//——---------------------————
———————ctrl
文件.m
----------------------------------------
ViewController.m
#import "ViewController.h" #import "dataModel.h" #import "MyTableViewCell.h" #import "frameModel.h" @interface ViewController () @property ( nonatomic, strong) NSArray *frameData; @end @implementation ViewController - ( void)viewDidLoad { [ super viewDidLoad]; } - ( NSInteger)tableView:( UITableView *)tableView numberOfRowsInSection:( NSInteger)section { return self. frameData. count; } - ( UITableViewCell *)tableView:( UITableView *)tableView cellForRowAtIndexPath:( NSIndexPath *)indexPath { //1. 创建 cell MyTableViewCell *cell = [ MyTableViewCell cellWithTableView:tableView]; //2. 调用 set 方法设置数据、设置 frame cell. frameModel = self. frameData[indexPath. row]; //3. 返回 cell return cell; } // 懒加载 - ( NSArray *)frameData { if ( _frameData == nil) { // 初始化 // 1. 获得 plist 的全路径 NSString *path = [[ NSBundle mainBundle] pathForResource: @"statuses.plist" ofType: nil]; // 2. 加载数组 NSArray *dictArray = [ NSArray arrayWithContentsOfFile:path]; // 3. 将 dictArray 里面的所有字典转成模型对象 , 放到新的数组中 NSMutableArray *arrM = [ NSMutableArray array]; for ( NSDictionary *dict in dictArray) { // 3.1. 创建 dataModel 模型对象 dataModel *DM = [ dataModel modelWithDict:dict]; // 3.2. 创建 frameModel 模型对象 frameModel *FM = [[ frameModel alloc] init]; // 3.3. 将数据模型交给 frame 模型 FM. model = DM; // 3.4. 添加模型对象到数组中 [arrM addObject:FM]; } // 4. 赋值 _frameData = arrM; } return _frameData; } #pragma mark - 实现代理方法 - ( CGFloat)tableView:( UITableView *)tableView heightForRowAtIndexPath:( NSIndexPath *)indexPath { // 取出这行对应的 frame 模型 frameModel *FM = self. frameData[indexPath. row]; return FM. cellHight; }
@end
|