码迷,mamicode.com
首页 > 移动开发 > 详细

iOS开发——UI篇OC篇&UITableView简单封装

时间:2015-06-19 20:13:37      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

UITableView简单封装

 

UITableView时iOS开发中使用最多也是最重的一个UI空间,其实在App Store里面的%80以上的应用都用到了这个控件,所以就给大家介绍一下,前面的文章中也提到了,在后面的文章中将会详细解释。

当然这篇文档并不是介绍证明去使用它,只是说说怎么去封装活着更好的使用。

这里主要是关于UItableView中Cell中的多功能实现

一:新建一个模型数据,用于Cell的实现

 1 /**
 2  *  覆盖系统的枚举
 3  */
 4 typedef enum {
 5     CellItemTypeNone,                   // don‘t show any accessory view
 6     CellItemTypeDisclosureIndicator,    // regular chevron. doesn‘t track
 7     CellItemTypeDetailDisclosureButton, // info button w/ chevron. tracks
 8     CellItemTypeCheckmark,              // checkmark. doesn‘t track
 9     CellItemTypeDetailButton NS_ENUM_AVAILABLE_IOS(7_0), // info button. tracks
10     
11     CellItemTypeSwitch//扩充
12     /**
13      *  当以后我们使用tableView的时候,如果每个Cell或者Cell的右边有系统Cell存在的控件或者View的时候,我们只要先在这里定义对应的控件或者View
14      */
15     
16 }CellItemType;
17 
18 @interface iCocosModel : NSObject
19 
20 
21 //文字标题
22 @property (nonatomic, copy) NSString *title;
23 
24 //子标题
25 @property (nonatomic, copy) NSString *subtitle;
26 
27 //类名,点击对应的行显示对应类的控制器
28 @property (nonatomic, copy) NSString *className;
29 
30 //类型:比如事剪头还是按钮还是Switch活着其他的
31 @property (nonatomic, assign) CellItemType cellItemType;
32 
33 /**
34  *  初始化方法
35  */
36 +(id)itemWithTitle:(NSString *)title cellItemType:(CellItemType)cellItemType;

模型数据方法的实现

 1 /**
 2  *  初始化方法
 3  */
 4 
 5 +(id)itemWithTitle:(NSString *)title cellItemType:(CellItemType)cellItemType
 6 {
 7     iCocosModel *item = [[iCocosModel alloc] init];
 8     /**
 9      *  转为对应的模型
10      */
11     
12     item.title = title;
13     
14     item.cellItemType = cellItemType;
15     
16     
17     //返回每一个项
18     return item;
19     
20 }

完成了这一步,我们使用起来就非常简单了。

 1 #import "iCocosViewController.h"
 2 /**
 3  *  对应的模型类
 4  */
 5 #import "iCocosModel.h"
 6 
 7 
 8 @interface iCocosViewController ()
 9 {
10     NSArray *_cellItems;
11 }
12 @end
13 
14 @implementation iCocosViewController
15 
16 
17 //重写这个方法实现分组:改变默认是形式
18 -(id)init
19 {
20     return  [self  initWithStyle:UITableViewStyleGrouped];
21 }
22 
23 - (void)viewDidLoad
24 {
25     [super viewDidLoad];
26 
27     [self addCellItems];
28 }

这里时最重要的部分:

 1 -(void)addCellItems
 2 {
 3     /**
 4      *  这里就是使用的实现方法,我要添加什么行就先创建行,然后放到_cellitems数组中
 5      */
 6     iCocosModel *note = [iCocosModel itemWithTitle:@"通知设置" cellItemType:CellItemTypeDisclosureIndicator];
 7     
 8     iCocosModel *update = [iCocosModel itemWithTitle:@"上传高清图片" cellItemType:CellItemTypeSwitch];
 9     iCocosModel *photo = [iCocosModel itemWithTitle:@"照片水印" cellItemType:CellItemTypeSwitch];
10 
11     _cellItems = @[
12                    @[note],
13                    @[update, photo]
14                    ];
15     
16 }

代理方法的实现

 1 #pragma mark TableView代理方法
 2 
 3 //节数
 4 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 5 {
 6     //数组的个数,里面可能还有字典
 7     return _cellItems.count;
 8 }
 9 
10 //行数
11 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
12 {
13     //数组中每个元素(字典)包含子元素的个数
14     NSArray *array = _cellItems[section];
15     return array.count;
16 }
17 //对应的数据
18 #pragma mark 每当有一个cell进入视野范围内就会调用,返回当前这行显示的cell
19 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
20 {
21     static NSString *ID = @"Cell";
22     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
23     if (cell == nil) {
24         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
25     }
26 
27     
28     /**
29      *  从模型中取得子元素
30      */
31     iCocosModel *item = _cellItems[indexPath.section][indexPath.row];
32 
33     cell.textLabel.text = item.title;
34     
35     if (item.cellItemType == CellItemTypeSwitch) {
36         cell.accessoryView = [[UISwitch alloc] init];
37     } else {
38         cell.accessoryType = item.cellItemType;
39     }
40     
41     return cell;
42 }
43 
44 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
45 {
46     
47     
48     
49 }

 

如果你到了这一步说明你已经成功了,当然步骤是死的,人是活的,你也可以实现更加复杂活着实用的功能,在后面的文章我将会对他进行更加深入的封装,最后不管到了那里都可拿来用,这才是一个真正的开发者,不是吗!

 

iOS开发——UI篇OC篇&UITableView简单封装

标签:

原文地址:http://www.cnblogs.com/iCocos/p/4589599.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!