iOS開發UI篇—使用純代碼自定義UItableviewcell實現一個簡單的微博界面佈局

iOS開發UI篇—使用純代碼自定義UItableviewcell實現一個簡單的微博界面佈局緩存

1、實現效果ide

 

2、使用純代碼自定義一個tableview的步驟佈局

1.新建一個繼承自UITableViewCell的類字體

2.重寫initWithStyle:reuseIdentifier:方法atom

添加全部須要顯示的子控件(不須要設置子控件的數據和frame,  子控件要添加到contentView中)spa

進行子控件一次性的屬性設置(有些屬性只須要設置一次, 好比字體\固定的圖片)代理

3.提供2個模型code

數據模型: 存放文字數據\圖片數據對象

frame模型: 存放數據模型\全部子控件的frame\cell的高度blog

4.cell擁有一個frame模型(不要直接擁有數據模型)

5.重寫frame模型屬性的setter方法: 在這個方法中設置子控件的顯示數據和frame 

6.frame模型數據的初始化已經採起懶加載的方式(每個cell對應的frame模型數據只加載一次)

3、文件結構和實現代碼

1.文件結構

 

2.實現代碼:

NJWeibo.h文件

複製代碼

#import <Foundation/Foundation.h>

@interface NJWeibo : 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;

- (id)initWithDict:(NSDictionary *)dict;
+ (id)weiboWithDict:(NSDictionary *)dict;
@end

 
 
複製代碼

NJWeibo.m文件

複製代碼

#import "NJWeibo.h"

@implementation NJWeibo

- (id)initWithDict:(NSDictionary *)dict
{
if (self = [super init]) {
[self setValuesForKeysWithDictionary:dict];
}
return self;
}

+ (id)weiboWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
}

@end

 
 
複製代碼

NJWeiboCell.h文件

複製代碼

#import <UIKit/UIKit.h>
@class NJWeiboFrame;

@interface NJWeiboCell : UITableViewCell
/**
* 接收外界傳入的模型
*/
//@property (nonatomic, strong) NJWeibo *weibo;

@property (nonatomic, strong) NJWeiboFrame *weiboFrame;

+ (instancetype)cellWithTableView:(UITableView *)tableView;
@end

 
 
複製代碼

NJWeiboCell.m文件

複製代碼

#import "NJWeiboCell.h"
#import "NJWeibo.h"
#import "NJWeiboFrame.h"

#define NJNameFont [UIFont systemFontOfSize:15]
#define NJTextFont [UIFont systemFontOfSize:16]

@interface NJWeiboCell ()
/**
* 頭像
*/
@property (nonatomic, weak) UIImageView *iconView;
/**
* vip
*/
@property (nonatomic, weak) UIImageView *vipView;
/**
* 配圖
*/
@property (nonatomic, weak) UIImageView *pictureView;
/**
* 暱稱
*/
@property (nonatomic, weak) UILabel *nameLabel;
/**
* 正文
*/
@property (nonatomic, weak) UILabel *introLabel;
@end

@implementation NJWeiboCell

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
// NSLog(@"cellForRowAtIndexPath");
static NSString *identifier = @"status";
// 1.緩存中取
NJWeiboCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
// 2.建立
if (cell == nil) {
cell = [[NJWeiboCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
return cell;
}


/**
* 構造方法(在初始化對象的時候會調用)
* 通常在這個方法中添加須要顯示的子控件
*/
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// 讓自定義Cell和系統的cell同樣, 一建立出來就擁有一些子控件提供給咱們使用
// 1.建立頭像
UIImageView *iconView = [[UIImageView alloc] init];
[self.contentView addSubview:iconView];
self.iconView = iconView;

// 2.建立暱稱
UILabel *nameLabel = [[UILabel alloc] init];
nameLabel.font = NJNameFont;
// nameLabel.backgroundColor = [UIColor redColor];
[self.contentView addSubview:nameLabel];
self.nameLabel = nameLabel;

// 3.建立vip
UIImageView *vipView = [[UIImageView alloc] init];
vipView.image = [UIImage imageNamed:@"vip"];
[self.contentView addSubview:vipView];
self.vipView = vipView;

// 4.建立正文
UILabel *introLabel = [[UILabel alloc] init];
introLabel.font = NJTextFont;
introLabel.numberOfLines = 0;
// introLabel.backgroundColor = [UIColor greenColor];
[self.contentView addSubview:introLabel];
self.introLabel = introLabel;

// 5.建立配圖
UIImageView *pictureView = [[UIImageView alloc] init];
[self.contentView addSubview:pictureView];
self.pictureView = pictureView;

}
return self;
}


- (void)setWeiboFrame:(NJWeiboFrame *)weiboFrame
{
_weiboFrame = weiboFrame;

// 1.給子控件賦值數據
[self settingData];
// 2.設置frame
[self settingFrame];
}


/**
* 設置子控件的數據
*/
- (void)settingData
{
NJWeibo *weibo = self.weiboFrame.weibo;

// 設置頭像
self.iconView.image = [UIImage imageNamed:weibo.icon];
// 設置暱稱
self.nameLabel.text = weibo.name;
// 設置vip
if (weibo.vip) {
self.vipView.hidden = NO;
self.nameLabel.textColor = [UIColor redColor];
}else
{
self.vipView.hidden = YES;
self.nameLabel.textColor = [UIColor blackColor];
}
// 設置內容
self.introLabel.text = weibo.text;

// 設置配圖
if (weibo.picture) {// 有配圖
self.pictureView.image = [UIImage imageNamed:weibo.picture];
self.pictureView.hidden = NO;
}else
{
self.pictureView.hidden = YES;
}
}
/**
* 設置子控件的frame
*/
- (void)settingFrame
{

// 設置頭像的frame
self.iconView.frame = self.weiboFrame.iconF;

// 設置暱稱的frame
self.nameLabel.frame = self.weiboFrame.nameF;

// 設置vip的frame
self.vipView.frame = self.weiboFrame.vipF;

// 設置正文的frame
self.introLabel.frame = self.weiboFrame.introF;

// 設置配圖的frame

if (self.weiboFrame.weibo.picture) {// 有配圖
self.pictureView.frame = self.weiboFrame.pictrueF;
}
}

/**
* 計算文本的寬高
*
* @param str 須要計算的文本
* @param font 文本顯示的字體
* @param maxSize 文本顯示的範圍
*
* @return 文本佔用的真實寬高
*/
- (CGSize)sizeWithString:(NSString *)str font:(UIFont *)font maxSize:(CGSize)maxSize
{
NSDictionary *dict = @{NSFontAttributeName : font};
// 若是未來計算的文字的範圍超出了指定的範圍,返回的就是指定的範圍
// 若是未來計算的文字的範圍小於指定的範圍, 返回的就是真實的範圍
CGSize size = [str boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;
return size;
}

@end

複製代碼

NJWeiboFrame.h文件

複製代碼

// 專門用來保存每一行數據的frame, 計算frame

#import <Foundation/Foundation.h>
@class NJWeibo;
@interface NJWeiboFrame : NSObject
/**
* 頭像的frame
*/
@property (nonatomic, assign) CGRect iconF;
/**
* 暱稱的frame
*/
@property (nonatomic, assign) CGRect nameF;
/**
* vip的frame
*/
@property (nonatomic, assign) CGRect vipF;
/**
* 正文的frame
*/
@property (nonatomic, assign) CGRect introF;
/**
* 配圖的frame
*/
@property (nonatomic, assign) CGRect pictrueF;
/**
* 行高
*/
@property (nonatomic, assign) CGFloat cellHeight;

/**
* 模型數據
*/
@property (nonatomic, strong) NJWeibo *weibo;
@end

複製代碼

NJWeiboFrame.m文件

複製代碼

#import "NJWeiboFrame.h"
#import "NJWeibo.h"
#define NJNameFont [UIFont systemFontOfSize:15]
#define NJTextFont [UIFont systemFontOfSize:16]


@implementation NJWeiboFrame


- (void)setWeibo:(NJWeibo *)weibo
{
_weibo = weibo;

// 間隙
CGFloat padding = 10;

// 設置頭像的frame
CGFloat iconViewX = padding;
CGFloat iconViewY = padding;
CGFloat iconViewW = 30;
CGFloat iconViewH = 30;
self.iconF = CGRectMake(iconViewX, iconViewY, iconViewW, iconViewH);

// 設置暱稱的frame
// 暱稱的x = 頭像最大的x + 間隙
CGFloat nameLabelX = CGRectGetMaxX(self.iconF) + padding;
// 計算文字的寬高
CGSize nameSize = [self sizeWithString:_weibo.name font:NJNameFont maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];

CGFloat nameLabelH = nameSize.height;
CGFloat nameLabelW = nameSize.width;
CGFloat nameLabelY = iconViewY + (iconViewH - nameLabelH) * 0.5;
self.nameF = CGRectMake(nameLabelX, nameLabelY, nameLabelW, nameLabelH);

// 設置vip的frame
CGFloat vipViewX = CGRectGetMaxX(self.nameF) + padding;
CGFloat vipViewY = nameLabelY;
CGFloat vipViewW = 14;
CGFloat vipViewH = 14;
self.vipF = CGRectMake(vipViewX, vipViewY, vipViewW, vipViewH);

// 設置正文的frame
CGFloat introLabelX = iconViewX;
CGFloat introLabelY = CGRectGetMaxY(self.iconF) + padding;
CGSize textSize = [self sizeWithString:_weibo.text font:NJTextFont maxSize:CGSizeMake(300, MAXFLOAT)];

CGFloat introLabelW = textSize.width;
CGFloat introLabelH = textSize.height;

self.introF = CGRectMake(introLabelX, introLabelY, introLabelW, introLabelH);

// 設置配圖的frame
CGFloat cellHeight = 0;
if (_weibo.picture) {// 有配圖
CGFloat pictureViewX = iconViewX;
CGFloat pictureViewY = CGRectGetMaxY(self.introF) + padding;
CGFloat pictureViewW = 100;
CGFloat pictureViewH = 100;
self.pictrueF = CGRectMake(pictureViewX, pictureViewY, pictureViewW, pictureViewH);

// 計算行高
self.cellHeight = CGRectGetMaxY(self.pictrueF) + padding;
}else
{
// 沒有配圖狀況下的行高
self.cellHeight = CGRectGetMaxY(self.introF) + padding;
}

}

/**
* 計算文本的寬高
*
* @param str 須要計算的文本
* @param font 文本顯示的字體
* @param maxSize 文本顯示的範圍
*
* @return 文本佔用的真實寬高
*/
- (CGSize)sizeWithString:(NSString *)str font:(UIFont *)font maxSize:(CGSize)maxSize
{
NSDictionary *dict = @{NSFontAttributeName : font};
// 若是未來計算的文字的範圍超出了指定的範圍,返回的就是指定的範圍
// 若是未來計算的文字的範圍小於指定的範圍, 返回的就是真實的範圍
CGSize size = [str boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;
return size;
}
@end

 
 
複製代碼

主控制器

NJViewController.m文件

複製代碼

#import "NJViewController.h"
#import "NJWeibo.h"
#import "NJWeiboCell.h"
#import "NJWeiboFrame.h"

@interface NJViewController ()
@property (nonatomic, strong) NSArray *statusFrames;
@end

@implementation NJViewController

#pragma mark - 數據源方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.statusFrames.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NJWeiboCell *cell = [NJWeiboCell cellWithTableView:tableView];
// 3.設置數據
cell.weiboFrame = self.statusFrames[indexPath.row];

// 4.返回
return cell;
}
#pragma mark - 懶加載
- (NSArray *)statusFrames
{
if (_statusFrames == nil) {
NSString *fullPath = [[NSBundle mainBundle] pathForResource:@"statuses.plist" ofType:nil];
NSArray *dictArray = [NSArray arrayWithContentsOfFile:fullPath];
NSMutableArray *models = [NSMutableArray arrayWithCapacity:dictArray.count];
for (NSDictionary *dict in dictArray) {
// 建立模型
NJWeibo *weibo = [NJWeibo weiboWithDict:dict];
// 根據模型數據建立frame模型
NJWeiboFrame *wbF = [[NJWeiboFrame alloc] init];
wbF.weibo = weibo;

[models addObject:wbF];
}
self.statusFrames = [models copy];
}
return _statusFrames;
}

#pragma mark - 代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// NSLog(@"heightForRowAtIndexPath");
// 取出對應航的frame模型
NJWeiboFrame *wbF = self.statusFrames[indexPath.row];
NSLog(@"height = %f", wbF.cellHeight);
return wbF.cellHeight;
}

- (BOOL) prefersStatusBarHidden
{
return YES;
}
@end

 
 
複製代碼

4、補充說明

因爲系統提供的tableview可能並不能知足咱們的開發需求,因此常常要求咱們可以自定義tableview。

自定義tableview有兩種方式,一種是使用xib建立,一種是使用純代碼的方式建立。

對於樣式同樣的tableview,一般使用xib進行建立,對於高度不同,內容也不徹底一致的一般使用純代碼進行自定義。

相關文章
相關標籤/搜索