0、在需要创建的地方,command+n创建
1、纯代码创建cell
1.1自定义等高cell的时候,子控件应该添加在initWithStyle方法中,子控件应该添加在contentView上
@interface gonggaoCell()
/** 标题 */
@property (nonatomic, weak) UILabel *gonggGaoTitleLabel;
@end
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
// self.contentView.backgroundColor = [UIColor redColor];
//如不需要变更可直接用initWithFrame来写位置,
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.textColor = main16Color(0x333333);
titleLabel.font = [UIFont systemFontOfSize:17.0f];
[self.contentView addSubview:titleLabel];
self.gonggGaoTitleLabel = titleLabel;
}
return self;
}
//设置位置
-(void)layoutSubviews
{
[super layoutSubviews];
self.gonggGaoTitleLabel.frame = CGRectMake(x, y, width, height);
}
1.2、纯代码自定义不等高cell
这样的样式cell,一般需要创建一个中间的model层,来计算各个控件的位置,
像这样
#import <UIKit/UIKit.h>
@interface gonggaomesaageModel : NSObject
/** 标题 */
@property (nonatomic, copy) NSString *noticetitle;
/** noticeid */
@property (nonatomic, copy) NSString *noticeid;
/** 正文(内容) */
@property (nonatomic, copy) NSString *noticedesc;
/** 已读 未读 */
@property (nonatomic, copy) NSString *noticeflag;
/** 时间 */
@property (nonatomic, copy) NSString *noticetime;
/** 消息总数 */
@property (nonatomic, copy) NSString *count;
/** 消息总数 */
@property (nonatomic, copy) NSString *unreadnums;
/** 消息总数 */
@property (nonatomic, copy) NSArray *items;
/** frame数据***/
/** 标题的frame */
@property (nonatomic, assign) CGRect titleFrame;
/** 标题的frame */
@property (nonatomic, assign) CGRect noticeflagFrame;
/** 正文的frame */
@property (nonatomic, assign) CGRect fengeFrame;
/** 正文的frame */
@property (nonatomic, assign) CGRect textconstentFrame;
/** 时间的frame */
@property (nonatomic, assign) CGRect shijianFrame;
/** 下部假分割的frame */
@property (nonatomic, assign) CGRect xiafengeFrame;
/** 查看详情Viewframe */
@property (nonatomic, assign) CGRect xiangqingFrame;
/** 查看详情的frame */
@property (nonatomic, assign) CGRect chakanFrame;
/** cell的高度 */
@property (nonatomic, assign) CGFloat cellHeight;
@end
创建cell,在cell.h文件中引入model
#import <UIKit/UIKit.h>
@class gonggaomesaageModel;
@interface gonggaoCell : UITableViewCell
@property(nonatomic , strong) gonggaomesaageModel * messageModel;
@end
cell.m实现
#import "gonggaoCell.h"
#import "gonggaomesaageModel.h"
@interface gonggaoCell()
/** 标题 */
@property (nonatomic, weak) UILabel *gonggGaoTitleLabel;
/** 分割线 */
@property (nonatomic, weak) UIView *fengeView;
@property (nonatomic, weak) UIView *dibuView;
/** 查看详情 */
@property (nonatomic, weak) UIView *xiangqingView;
/** 内容 */
@property (nonatomic, weak) UILabel *gonggGaoContentLabel;
/** 时间 */
@property (nonatomic, weak) UILabel *gonggGaoTimeLabel;
/** 查看详情*/
@property (nonatomic, weak) UILabel *chakanLabel;
@end
@implementation gonggaoCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
// self.contentView.backgroundColor = [UIColor redColor];
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.textColor = main16Color(0x333333);
titleLabel.font = [UIFont systemFontOfSize:17.0f];
[self.contentView addSubview:titleLabel];
self.gonggGaoTitleLabel = titleLabel;
UIView *fengeView = [[UIView alloc]init];
fengeView.backgroundColor = main16Color(0xEEEEEE);
[self.contentView addSubview:fengeView];
self.fengeView = fengeView;
UILabel *contentLabel = [[UILabel alloc] init];
contentLabel.font = [UIFont systemFontOfSize:17.0f];
contentLabel.numberOfLines = 0;
contentLabel.textColor = main16Color(0x666666);
[self.contentView addSubview:contentLabel];
self.gonggGaoContentLabel = contentLabel;
UILabel *TimeLabel = [[UILabel alloc] init];
TimeLabel.font = [UIFont systemFontOfSize:17.0f];
TimeLabel.textColor = main16Color(0xBBBBBB);
[self.contentView addSubview:TimeLabel];
self.gonggGaoTimeLabel = TimeLabel;
UIView *dibuView = [[UIView alloc]init];
dibuView.backgroundColor = main16Color(0xF5F5F5);
[self.contentView addSubview:dibuView];
self.dibuView = dibuView;
UIView *xiangqingView = [[UIView alloc]init];
xiangqingView.backgroundColor = mainWhiteColor;
[self.contentView addSubview:xiangqingView];
self.xiangqingView = xiangqingView;
UIView *fengexianView = [[UIView alloc]initWithFrame:CGRectMake(20, 0, mainScreenW-20, 1)];
fengexianView.backgroundColor = main16Color(0xEEEEEE);
[xiangqingView addSubview:fengexianView];
UILabel *chakanLabel = [[UILabel alloc] init];
chakanLabel.font = [UIFont systemFontOfSize:17.0f];
chakanLabel.textColor = main16Color(0x999999);
[xiangqingView addSubview:chakanLabel];
self.chakanLabel = chakanLabel;
}
return self;
}
-(void)setMessageModel:(gonggaomesaageModel *)messageModel
{
_messageModel = messageModel;
self.gonggGaoTitleLabel.text = messageModel.noticetitle;
self.gonggGaoContentLabel.text = messageModel.noticedesc;
self.gonggGaoTimeLabel.text = [self timeWithTimeIntervalString:messageModel.noticetime];
self.chakanLabel.text = @"查看详情 》";
}
-(void)layoutSubviews
{
[super layoutSubviews];
self.gonggGaoTitleLabel.frame = self.messageModel.titleFrame;
self.fengeView.frame = self.messageModel.fengeFrame;
self.gonggGaoContentLabel.frame = self.messageModel.textconstentFrame;
self.gonggGaoTimeLabel.frame = self.messageModel.shijianFrame;
self.dibuView.frame = self.messageModel.xiafengeFrame;
self.xiangqingView.frame = self.messageModel.xiangqingFrame;
self.chakanLabel.frame = self.messageModel.chakanFrame;
}
- (NSString *)timeWithTimeIntervalString:(NSString *)timeString
{
// 格式化时间
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
formatter.timeZone = [NSTimeZone timeZoneWithName:@"beijing"];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"YYYY-MM-dd"];
// 毫秒值转化为秒
NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]/ 1000.0];
NSString* dateString = [formatter stringFromDate:date];
return dateString;
}
@end