|
在该篇文章中主要介绍的是仿网易新闻的首页中不通的cell风格,自定义cell,自适应cell。此处用到了mvc模式。顺便给不知道mvc开发模式的同学粗略的介绍一下什么是mvc模式。
mvc就是model-view-controller,model就是数据模型,view就是展示数据的视图,controller就是控制器。其中model和view之间是通过controller联系在一起的。
第一步:创建数据模型的类NewsModel.h和NewsModel.m文件,此处我没有使用字典转模型,而是用了最原始的方法
#import <UIKit/UIKit.h>
@interface NewsModel : NSObject
//共用
@property (nonatomic,copy)
NSString *subId;//id
@property (nonatomic,copy)
NSString *type;//类型: 1标题日期
2网页 3人员职务;
@property (nonatomic,copy)
NSString *title;//标题/名称
//类型1和类型2共用
@property (nonatomic,copy)
NSString *imagePath;//图片网址
//类型1
@property (nonatomic,copy)
NSString *job;//职务
//类型2
@property (nonatomic,copy)
NSString *date;//日期
@end
第二部:创建NewsCell类的NewsCell.h和NewsCell.m文件,添加头文件NewsModel.h,声明属性
@property (nonatomic,strong)
UIImageView *iconView;
@property (nonatomic,strong)
UILabel *titleLabel;
@property (nonatomic,strong)
UILabel *subLabel;
@property (nonatomic,strong)
NewsModel *model;
第三部分:重写父类方法
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString
*)reuseIdentifier
{
self = [superinitWithStyle:style
reuseIdentifier:reuseIdentifier];
if (self) {
self.backgroundColor = [UIColorwhiteColor];
self.accessoryType =UITableViewCellAccessoryNone;
[selfcreateUI];
}
returnself;
}
第四部分:创建cell中对应的控件
- (void)createUI{
iconView = [[UIImageViewalloc]
init];
iconView.backgroundColor = [UIColorgrayColor];
[self.contentViewaddSubview:iconView];
titleLabel = [[UILabelalloc]
init];
titleLabel.textColor = [UIColorblackColor];
titleLabel.numberOfLines =0;
titleLabel.font = [UIFontsystemFontOfSize:15.0f];
[self.contentViewaddSubview:titleLabel];
subLabel = [[UILabelalloc]
init];
subLabel.textColor = [UIColorgrayColor];
subLabel.font = [UIFontsystemFontOfSize:13.0f];
[self.contentViewaddSubview:subLabel];
}
第五部分:添加数据
- (void)setModel:(NewsModel *)model{
_model = model;
if ([model.typeintegerValue] ==
3) {
[selfsetFirstCellWithModel:model];
}elseif ([model.typeintegerValue]
== 1){
[selfsetSecondCellWithModel:model];
}
}
#pragma mark - 类型3人员职务
- (void)setFirstCellWithModel:(NewsModel *)model{
iconView.frame =CGRectMake(10,10,
40,40);
iconView.layer.cornerRadius =iconView.frame.size.width/2;
iconView.clipsToBounds =YES;
if ([StringIsNullisBlankString:model.imagePath]
==NO) {
[iconViewsd_setImageWithURL:[NSURLURLWithString:model.imagePath]placeholderImage:[UIImageimageNamed:@"peopleHolderImage.jpg"]];
}
if ([StringIsNullisBlankString:model.title]
==NO) {
titleLabel.text = [NSStringstringWithFormat:@"%@",model.title];
titleLabel.frame =CGRectMake(CGRectGetMaxX(iconView.frame)+10,10,WIDTH-CGRectGetMaxX(iconView.frame)-50,20)
;
}
if ([StringIsNullisBlankString:model.job]
==NO) {
subLabel.textAlignment =NSTextAlignmentLeft;
subLabel.text = [NSStringstringWithFormat:@"%@",model.job];
subLabel.frame =CGRectMake(CGRectGetMaxX(iconView.frame)+10,CGRectGetMaxY(titleLabel.frame),CGRectGetWidth(titleLabel.frame),20);
}
self.frame =CGRectMake(0,0,
WIDTH,60);
self.accessoryType =UITableViewCellAccessoryDisclosureIndicator;
}
#pragma mark - 类型1标题日期
- (void)setSecondCellWithModel:(NewsModel *)model{
iconView.frame =CGRectMake(10,10,
60,60);
if ([StringIsNullisBlankString:model.imagePath]
==NO) {
[iconViewsd_setImageWithURL:[NSURLURLWithString:model.imagePath]placeholderImage:[UIImageimageNamed:@"firstHolderimage.jpg"]];
}else{
iconView.frame =CGRectZero;
}
if ([StringIsNullisBlankString:model.title]
==NO) {
titleLabel.text = [NSStringstringWithFormat:@"%@",model.title];
//
// CGSize size = [model.title sizeWithfont:[UIFont systemFontOfSize:15.0f] maxSize:CGSizeMake(WIDTH-CGRectGetMaxX(iconView.frame)-20, CGFLOAT_MAX)];
titleLabel.frame =CGRectMake(CGRectGetMaxX(iconView.frame)+10,10,WIDTH-CGRectGetMaxX(iconView.frame)-20,40)
;
}
if ([StringIsNullisBlankString:model.date]
==NO) {
subLabel.textAlignment =NSTextAlignmentRight;
subLabel.text = [NSStringstringWithFormat:@"%@",model.date];
subLabel.frame =CGRectMake(CGRectGetMaxX(iconView.frame)+10,CGRectGetMaxY(titleLabel.frame),CGRectGetWidth(titleLabel.frame),20);
}
self.frame =CGRectMake(0,0,
WIDTH,CGRectGetMaxY(subLabel.frame)+10);
self.accessoryType =UITableViewCellAccessoryNone;
}
第六部分:tableView的代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
returnself.dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath
*)indexPath{
您知道如何返回cell的高度吗?不懂的请给我留言?
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath{
NewsCell *cell = [tableViewdequeueReusableCellWithIdentifier:@"cell"];
if (cell ==nil) {
cell = [[NewsCellalloc]
initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:@"cell"];
}
cell.model =self.dataArray[indexPath.row];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return0.01;
}
要demo的请留言
|