在实际项目中,由于项目需要,很多时候我们需要自己封装cell。假设我们要上传的cell是微博一个用户的状态。那这个cell就需要包含头像、用户名、vip、微博内容以及微博图片。
在Xcode中新建项目,新建一个类StatusCell继承至UITableViewCell。假设我们已经自定义了一个模型statuses这个类。
在StatusCell.h文件中
#import <UIKit/UIKit.h>
@class statuses;
@interface StatusCell : UITableViewCell
@property(nonatomic,strong) statuses *status;
@end
在StatusCell.m文件中
#import "StatusCell.h"
#import "statuses.h"
#define YFNameFont [UIFont systemFontOfSize:14] //定义一个字体大小的宏
在StatusCell这个类中添加成员变量
@interface StatusCell()
@property(nonatomic,weak)UIImageView *iconView;
@property(nonatomic,weak)UILabel *nameView;
@property(nonatomic,weak)UIImageView *vipView;
@property(nonatomic,weak)UILabel *textView;
@property(nonatomic,weak)UIImageView *pictureView;
@end
@implementation StatusCell
重写cell的init方法,在cell添加子控件。
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if(self){
//1.icon
UIImageView *iconView = [[UIImageView alloc] init];
[self.contentView addSubview:iconView];
self.iconView = iconView;
//2.name
UILabel *nameView = [[UILabel alloc] init];
nameView.font = YFNameFont;
[self.contentView addSubview:nameView];
self.nameView = nameView;
//3.vip
UIImageView *vipView = [[UIImageView alloc] init];
[self.contentView addSubview:vipView];
self.vipView = vipView;
//4.text
UILabel *textView = [[UILabel alloc] init];
textView.font = YFNameFont;
textView.numberOfLines = 0;
[self.contentView addSubview:textView];
self.textView = textView;
//5.picture
UIImageView *pictureView = [[UIImageView alloc] init];
[self.contentView addSubview:pictureView];
self.pictureView = pictureView;
}
return self;
}
添加子控件后,设置子控件的frame
-(void)setStatus:(statuses *)status{
_status = status;
//1.设置数据
[self settingData];
//2.设置frame
[self settingFrame];
}
//获取字体的长度和高度
-(CGSize)sizeWithtext:(NSString *) text font:(UIFont *)font maxSize:(CGSize)maxSize{
NSDictionary *attr = @{NSFontAttributeName :font};
return [self.status.name boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attr context:nil].size;
}
//设置frame
-(void)settingFrame{
CGFloat padding = 10;
CGFloat iconX = padding;
CGFloat iconY = padding;
CGFloat iconW = 30;
CGFloat iconH = 30;
self.iconView.frame = CGRectMake(iconX, iconY, iconW, iconH);
CGFloat nameX = CGRectGetMaxX(self.iconView.frame) + padding;
CGSize nameSize = [self sizeWithtext:self.status.name font:YFNameFont maxSize:CGSizeMake(MAXFLOAT,MAXFLOAT)];
CGFloat nameY = iconY + (iconH - nameSize.height) * 0.5;
self.nameView.frame = CGRectMake(nameX, nameY, nameSize.width, nameSize.height);
CGFloat vipX = CGRectGetMaxX(self.nameView.frame) + padding;
CGFloat vipY = nameY;
CGFloat vipW = 14;
CGFloat vipH = 14;
self.vipView.frame = CGRectMake(vipX, vipY, vipW, vipH);
CGFloat textX = iconX;
CGFloat textY =CGRectGetMaxX(self.iconView.frame) + padding;
CGSize textSize = [self sizeWithtext:self.status.text font:YFNameFont maxSize:CGSizeMake(300,MAXFLOAT)];
self.textView.frame = CGRectMake(textX, textY, textSize.width, textSize.height);
if(self.status.picture){
CGFloat picX = textX;
CGFloat picY = CGRectGetMaxY(self.textView.frame) + padding;
CGFloat picW = 100;
CGFloat picH = 100;
self.pictureView.frame = CGRectMake(picX, picY, picW, picH);
}
}
这样,一个cell的封装就搞定了。