首页 > 代码库 > 开发进阶19_通过代码自定义Cell
开发进阶19_通过代码自定义Cell
//注册一个xib文件
//如果tableView通过@"cell"这个标志去缓存池中没有渠道可循环利用的cell,就会加载MyCell1.xib文件来创建cell(IOS5.0)
[self.tableView registerNib:[UINib nibWithNibName:@"MyCell" bundle:nil] forCellReuseIdentifier:@"Cell"];
//如果cell是通过类文件创建的
//如果tableView通过@"cell"这个标志去缓存池中没有渠道可循环利用的cell,就会通过类MyCell文件来创建cell(IOS6.0)
[self.tableView registerClass:[MyCell class] forCellReuseIdentifier:@"Cell"];
#import "ViewController.h"
#import "WeiboCell.h"
#import "Weibo.h"
#import "WeiboFrame.h"
@interface ViewController ()
{
NSMutableArray *_weiboFrames;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *arr = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"weibo.plist" ofType:nil]];
_weiboFrames = [NSMutableArray array];
for(NSDictionary *dict in arr){
WeiboFrame *weiboF = [[WeiboFrame alloc] init];
weiboF.weibo = [Weibo weiboWithDict:dict];
[_weiboFrames addObject:weiboF];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _weiboFrames.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ID = @"weibo";
WeiboCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if(cell == nil){
cell = [[WeiboCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
cell.weiboFrame = _weiboFrames[indexPath.row];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [_weiboFrames[indexPath.row] cellHeight];
}
@end
@interface Weibo : NSObject
@property (nonatomic, copy) NSString *content;//内容
@property (nonatomic, copy) NSString *time;//时间
@property (nonatomic, copy) NSString *source;//来源
@property (nonatomic, copy) NSString *icon;//头像
@property (nonatomic, copy) NSString *name;//昵称
@property (nonatomic, copy) NSString *image;//配图
@property (nonatomic, assign) BOOL vip;
+ (id)weiboWithDict:(NSDictionary *)dict;
- (id)initWithDict:(NSDictionary *)dict;
@end
@implementation Weibo
+ (id)weiboWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
}
- (id)initWithDict:(NSDictionary *)dict
{
if(self = [super init]){
self.name = dict[@"name"];
self.time = dict[@"time"];
self.vip = [dict[@"vip"] boolValue];
self.source = dict[@"source"];
self.image = dict[@"image"];
self.icon = dict[@"icon"];
self.content = dict[@"content"];
}
return self;
}
@end
@class WeiboFrame;
@interface WeiboCell : UITableViewCell
@property (nonatomic, weak) WeiboFrame *weiboFrame;
@end
@interface WeiboCell ()
{
UIImageView *_icon;
UILabel *_name;
UIImageView *_vip;
UILabel *_time;
UILabel *_source;
UILabel *_content;
UIImageView *_image;
}
@end
@implementation WeiboCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if(self){
//1.头像
_icon = [[UIImageView alloc] init];
[self.contentView addSubview:_icon];
//2.昵称
_name = [[UILabel alloc] init];
_name.font = kNameFont;
[self.contentView addSubview:_name];
//3.会员图标
_vip = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"05.jpg"]];
[self.contentView addSubview:_vip];
//4.时间
_time = [[UILabel alloc] init];
_time.font = kTimeFont;
_time.textColor = [UIColor grayColor];
[self.contentView addSubview:_time];
//5.来源
_source = [[UILabel alloc] init];
_source.font = kSourceFont;
_source.textColor = [UIColor grayColor];
[self.contentView addSubview:_source];
//6.微博正文
_content = [[UILabel alloc] init];
_content.font = kContentFont;
_content.numberOfLines = 0;//自动换行
[self.contentView addSubview:_content];
//7.配图
_image = [[UIImageView alloc] init];
[self.contentView addSubview:_image];
}
return self;
}
-(void)setWeiboFrame:(WeiboFrame *)weiboFrame
{
_weiboFrame = weiboFrame;
[self settingData];
[self settingSubViewFrame];
}
- (void)settingSubViewFrame
{
//1.头像
_icon.frame = _weiboFrame.iconF;
//2.昵称
_name.frame = _weiboFrame.nameF;
//3.vip
_vip.frame = _weiboFrame.vipF;
//4.时间
_time.frame = _weiboFrame.timeF;
//5.来源
_source.frame = _weiboFrame.sourceF;
//6.正文
_content.frame = _weiboFrame.contentF;
//7.配图
if(_weiboFrame.weibo.image)
{
_image.frame = _weiboFrame.imageF;
}
}
- (void)settingData
{
Weibo *weibo = _weiboFrame.weibo;
//1.头像
_icon.image = [UIImage imageNamed:weibo.icon];
//2.昵称
_name.text = weibo.name;
if(weibo.vip){
_name.textColor = [UIColor redColor];
}else{
_name.textColor = [UIColor blackColor];
}
//3.会员图标
_vip.hidden = !weibo.vip;
//4.时间
_time.text = weibo.time;
//5.来源
_source.text = [NSString stringWithFormat:@"来自%@", weibo.source ];
//6.正文
_content.text = weibo.content;
//7.配图
if(weibo.image){
_image.hidden = NO;
_image.image = [UIImage imageNamed:weibo.image];
}else{
_image.hidden = YES;
}
}
@end
#define kNameFont [UIFont systemFontOfSize:15]
#define kTimeFont [UIFont systemFontOfSize:12]
#define kSourceFont [UIFont systemFontOfSize:12]
#define kContentFont [UIFont systemFontOfSize:15]
@class Weibo;
@interface WeiboFrame : NSObject
@property (nonatomic, assign, readonly) CGRect iconF;//头像
@property (nonatomic, assign, readonly) CGRect nameF;//用户名
@property (nonatomic, assign, readonly) CGRect vipF;//vip
@property (nonatomic, assign, readonly) CGRect timeF;//时间
@property (nonatomic, assign, readonly) CGRect sourceF;//来源
@property (nonatomic, assign, readonly) CGRect contentF;//正文
@property (nonatomic, assign, readonly) CGRect imageF;//图片
//一个cell的高度
@property (nonatomic, assign, readonly) CGFloat cellHeight;
//数据模型
@property (nonatomic, strong) Weibo *weibo;
@end
#define kCellBorder 10 //边距
#define kIconWH 40 //头像宽高
#define kVipWH 20 //vip图标的宽高
#define kImageWH 80 //图片宽高
@implementation WeiboFrame
- (void)setWeibo:(Weibo *)weibo
{
_weibo = weibo;
//1.头像
CGFloat iconX = kCellBorder;
CGFloat iconY = kCellBorder;
_iconF = CGRectMake(iconX, iconY, kIconWH, kIconWH);
//2.昵称
CGFloat nameX = CGRectGetMaxX(_iconF) + kCellBorder;
CGFloat nameY = iconY;
CGSize nameSize = [_weibo.name sizeWithFont:kNameFont];
_nameF = CGRectMake(nameX, nameY, nameSize.width, nameSize.height);
//3.vip
CGFloat vipX = CGRectGetMaxX(_nameF) + kCellBorder;
CGFloat vipY = nameY;
_vipF = CGRectMake(vipX, vipY, kVipWH, kVipWH);
//4.时间
CGFloat timeX = nameX;
CGFloat timeY = CGRectGetMaxY(_nameF) + kCellBorder;
CGSize timeSize = [_weibo.time sizeWithFont:kTimeFont];
_timeF = CGRectMake(timeX, timeY, timeSize.width, timeSize.height);
//5.来源
CGFloat sourceX = CGRectGetMaxX(_timeF) + kCellBorder;
CGFloat sourceY = timeY;
NSString *sourceText = [NSString stringWithFormat:@"来自%@",_weibo.source];
CGSize sourceSize = [sourceText sizeWithFont:kSourceFont];
_sourceF = CGRectMake(sourceX, sourceY, sourceSize.width, sourceSize.height);
//6.正文
CGFloat contentX = iconX;
CGFloat contentY = MAX(CGRectGetMaxY(_timeF), CGRectGetMaxY(_iconF) + kCellBorder);
CGFloat contentW = 375 - 2 * kCellBorder;
//计算文字尺寸(显示文字的宽度)
CGSize contentSize = [_weibo.content sizeWithFont:kContentFont constrainedToSize:CGSizeMake(contentW, MAXFLOAT)];
_contentF = CGRectMake(contentX, contentY, contentW, contentSize.height);
//7.配图
if(_weibo.image)
{
CGFloat imageX = contentX;
CGFloat imageY = CGRectGetMaxY(_contentF) + kCellBorder;
_imageF = CGRectMake(imageX, imageY, kImageWH, kImageWH);
_cellHeight = CGRectGetMaxY(_imageF) + kCellBorder;
}else{
_cellHeight = CGRectGetMaxY(_contentF) + kCellBorder;
}
}
@end
开发进阶19_通过代码自定义Cell