三种cell的自定义的方法

(一)纯代码实现自定义cell

1.主要目的实现在创建cell的时候,只用执行两个步骤

1)类方法创建cell(将复用机制,和自定义cell控件,控件的位置封装到cell的类方法中,这里注意要添加一个参数,就是cell是那个tableView的cell这个参数)

2)向cell传递一个数据模型,既可以实现cell的数据更新(既在cell类模型数据的set方法中,实现cell中数据的更新)

3)如果cell中有点击事件,可以通过代理的方式向主控制器中传递事件

 2.关键代码

1 #import <UIKit/UIKit.h>
2 @class IWStatusFrame;
3 @interface IWStatusCell : UITableViewCell
4 + (instancetype)cellWithTableView:(UITableView *)tableView;
5 @property (nonatomic, strong) StatusFrame *statusFrame;
6 @end
StatusCell.h
 1 #import "StatusCell.h"
 2 #import "StatusFrame.h"
 3 
 4 @interface IWStatusCell()
 5 /** 头像 */
 6 @property (nonatomic, weak) UIImageView *iconView;
 7 /** 顶部的view */
 8 @property (nonatomic, weak) UILable *textLabel;
 9 @end
10 
11 @implementation IWStatusCell
12 //这个类方法主要是对复用机制进行封装
13 + (instancetype)cellWithTableView:(UITableView *)tableView
14 {
15     static NSString *ID = @"status";
16     IWStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
17     if (cell == nil) {
18         cell = [[IWStatusCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
19     }
20     return cell;
21 }
22 //重写此方法是对cell内部UI布局进行自定义
23 - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
24 {
25     self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
26     if (self) {
27         // 1.添加原创微博内部的子控件
28         [self setupOriginalSubviews]  ;
29    }
30     return self;
31 }
32 
33 /**
34  *  添加原创微博内部的子控件(仅仅只是实现控件的初始化以及添加到cell上)
35  */
36 - (void)setupOriginalSubviews
37 {   
38     /** 1.头像*/
39     UIImageView *iconView = [[UIImageView alloc] init];
40     iconView.image = [UIImage resizedImageWithName:@"timeline_card_top_background"];
41     [self.contentView addSubview:topView];
42    self.iconView =iconView;
43     
44     /** 2.文字*/
45    UILabel * textLabel =[[UILabel alloc] init];
46    textLabel.font =[UIFont systemFont:15.0f];
47    [self.contentView addSubview:textLabel];
48   self.textLabel = textLabel;
49 }
50 
51 //在数据模型的set方法中实现cell的内容的更新和布局
52 /**
53  *  传递模型数据,根据数据信息实现cell的内容的更新以及frame的确定
54  */
55 - (void)setStatusFrame:(IWStatusFrame *)statusFrame
56 {
57     _statusFrame = statusFrame;
58     //1.更新数据信息
59    _icon.image=[UIIamge imageNamed:statusFrame.image];
60    _textLabel .text = statusFrame.text;
61 
62    //2.设置控件的frame    
63   _icon.image.frame = statusFrame.iconF;
64   _textLabel.frame = statusFrame.textLabelF;
65 }
StatusCell.m

(二)xib实现自定义cell

1.思路:

1)创建自定义cell时,将xib勾选上,点击create之后,就后创建出cell.h,cell.m,cell.xib三个文件

2)在xib中实现cell的布局以及适配

3)在.h文件中,提供一个创建cell对象的类方法,以及一个设置cell内容的数据模型

@property(nonatomic,strong)GPNews * news;

+(instancetype)cell名WithTableView:(UITableView *)tableView;

4)将xib文件中的控件属性进行连线,这样才能在.m文件中拿到这些属性,进而设置其内容

5)在.m文件中,实现cell的从类方法(从xib文件中注册)以及,数据模型的set方法中实现cell内容的更新

2.关键代码

 1 //由于是表单行所以这里需要用注册方法实例化该类的对象
 2 +(id)largeCellWithTableView:(UITableView *)tableView
 3 {
 4    
 5     //1.注册
 6 //    1.1这里获得的是类名;作为重用的标志符和nib方法加载的xib的名字
 7     NSString * className = NSStringFromClass([self class]);
 8     UINib * nib = [UINib nibWithNibName:className bundle:nil];
 9 //     1.2这里是使用tableView的对象方法,注册一个带有类名这个标志的cell对象
10     [tableView registerNib:nib forCellReuseIdentifier:className];
11     //2.返回 cell 对象
12     return [tableView dequeueReusableCellWithIdentifier:className]
13     
14 }
15 
16 - (void)setNews:(GPNews *)news
17 {
18 // 1.这句是最简单的news成员变量的set方法赋值
19     _news = news;
20 // 2.重写set方法的其他目的
21     //2.1.更新 UI 数据
22       // 这句是多态的一种实现,将news下转为其子类对象
23     GPLarge * large = (GPLarge *)news;
24       //接下来的四句就是对控件中UI数据的更新赋值
25     self.titleLabel.text = large.title;
26     self.pictureImageView.image = [UIImage imageNamed:large.picture];
27     self.sourceLable.text = large.source;
28     self.timeLabel.text = large.time;
29     
30 //3.更新一下 Frame 值
31 #warning numberOfLines = 0 Label 才能够自动进行换行
32     self.titleLabel.numberOfLines = 0;
33     self.titleLabel.frame = large.titleFrame;
34     self.pictureImageView.frame = large.pictureFrame;
35     self.sourceLable.frame = large.sourceFrame;
36     self.timeLabel.frame = large.timeFrame;
37 }
XibWithCell.m

(三)storyboard实现自定义cell(动态cell)

1.好处:可以在一个storyboard上的tableViewController存放多种cell,通过cell的identifier来加载不同类型风格的cell

2.实现思路:

1)通过在storyboard上拖拽TableViewController,便可在其上自带的cell进行内容布局

2)另一边,通过创建cell与其关联,补充说明其cell内容的设置,通过传递数据模型,在数据模型的set方法中对cell内容数据进行更新

3)在返回值为tableViewCell的数据源方法中,即可更具指定的cell的identifier来创建相应的cell,其已经有了相应的复用机制了,无需用代码实现

3.关键代码

 1 #import "MJAppCell.h"
 2 #import "MJApp.h"
 3 
 4 @interface MJAppCell()
 5 @property (weak, nonatomic) IBOutlet UIImageView *iconView;
 6 @property (weak, nonatomic) IBOutlet UILabel *nameView;
 7 @property (weak, nonatomic) IBOutlet UIButton *downloadView;
 8 @property (weak, nonatomic) IBOutlet UILabel *introView;
 9 - (IBAction)downloadClick:(UIButton *)btn;
10 @end
11 
12 @implementation MJAppCell
13 
14 - (void)setApp:(MJApp *)app
15 {
16     _app = app;
17     
18     self.iconView.image = [UIImage imageNamed:app.icon];
19     self.nameView.text = app.name;
20     self.introView.text = [NSString stringWithFormat:@"大小:%@ | 下载量:%@", app.size, app.download];
21     
22     // 覆盖按钮的状态
23     self.downloadView.enabled = (self.app.isDownloaded == NO);
24 }
25 /**
26  *  点击了下载按钮
27  */
28 - (IBAction)downloadClick:(UIButton *)btn {
29     // 1.让按钮失效
30     self.app.downloaded = YES;
31     btn.enabled = NO;
32     
33     // 2.通知代理
34     if ([self.delegate respondsToSelector:@selector(appCellDidClickedDownloadBtn:)]) {
35         [self.delegate appCellDidClickedDownloadBtn:self];
36     }
37 }
38 @end
AppCell.m
1 ViewController.m
2 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
3 {
4     MJAppCell *cell = [tableView dequeueReusableCellWithIdentifier:@"app"];
5     cell.delegate = self;
6     cell.app = self.apps[indexPath.row];
7     return cell;
8 }
ViewController3.m

(四)storyboard实现自定义cell(静态cell)

1.使用场景:固定不变的一些cell(比如设置界面等等)

2.实现思路:

1)直接使用利用storyboard拖拽一个cell出来,设置tableView的content 为stain cells;

2)设置相关的图片,文字信息,关联视图控制器,就可以实现效果

3.经验:静态单元格,我们可以先搞定一组,再设置组数,(这样每组可以拷贝第一组的设置,节省操作)

 

 

 

和客户

 

posted @ 2016-09-07 19:27  许霞  阅读(1446)  评论(0编辑  收藏  举报