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文件
NJWeibo.m文件
NJWeiboCell.h文件
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文件
主控制器
NJViewController.m文件
4、補充說明
因爲系統提供的tableview可能並不能知足咱們的開發需求,因此常常要求咱們可以自定義tableview。
自定義tableview有兩種方式,一種是使用xib建立,一種是使用純代碼的方式建立。
對於樣式同樣的tableview,一般使用xib進行建立,對於高度不同,內容也不徹底一致的一般使用純代碼進行自定義。