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

iOS开发——开发必备OC篇&UITableView设置界面完整封装(四)

时间:2015-08-13 17:53:15      阅读:281      评论:0      收藏:0      [点我收藏+]

标签:

 

设置界面完整封装(四)

简单MVC实现UITableView设置界面完善封装及拓展使用

关于使用和拓展,

其实基本上就是同UItableView,知识讲数据改一下就可以

 

拓展使用

 

1:首先定义一个数组用来装组的模型

 

// 总共的组数

@property (nonatomic, strong) NSMutableArray *groups;

 

 

2:懒数组

 

 1 - (NSMutableArray *)groups
 2 
 3 {
 4 
 5     if (_groups == nil) {
 6 
 7         _groups = [NSMutableArray array];
 8 
 9     }
10 
11     return _groups;
12 
13 }
14 
15  

 

3:调用添加组的方法

 

 1 - (void)viewDidLoad {
 2 
 3     [super viewDidLoad];
 4 
 5     
 6 
 7     // 添加第0组
 8 
 9     [self setUpGroup0];
10 
11     
12 
13 }

 

 

4:设置组类型

 

1 - (instancetype)init
2 
3 {
4 
5     return [self initWithStyle:UITableViewStyleGrouped];
6 
7 }
8 
9  

 

 

5:添加对应的行活着组类型

 

 1 // 添加第0组
 2 
 3 - (void)setUpGroup0
 4 
 5 {
 6 
 7     // 创建行模型
 8 
 9     // 开奖推送
10 
11     iCocosSettingArrowItem *item = [iCocosSettingArrowItem itemWithImage:nil title:@"开奖推送"];
12 
13     item.descVc = [UIViewController class];
14 
15     
16 
17     // 比分直播
18 
19     iCocosSettingArrowItem *item1 = [iCocosSettingArrowItem itemWithImage:nil title:@"比分直播"];
20 
21     // 中奖动画
22 
23     iCocosSettingArrowItem *item2 = [iCocosSettingArrowItem itemWithImage:nil title:@"中奖动画"];
24 
25     // 购彩提醒
26 
27     iCocosSettingArrowItem *item3 = [iCocosSettingArrowItem itemWithImage:nil title:@"购彩提醒"];
28 
29        // Items:存储当前数组有多少行模型
30 
31     // 创建一个组模型,描述第0组
32 
33     iCocosGroupItem *group = [iCocosGroupItem groupWithItems:@[item,item1,item2,item3]];
34 
35  
36 
37     
38 
39     // 添加组模型到groups数组,有多少个组模型就有多少组
40 
41     [self.groups addObject:group];
42 
43 }
44 
45  

 

6:实现数据元和代理方法

 

  1 #pragma mark - 数据源
  2 
  3 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  4 
  5 {
  6 
  7     return self.groups.count;
  8 
  9 }
 10 
 11  
 12 
 13 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 14 
 15 {
 16 
 17     // 取出当前的组模型
 18 
 19     iCocosGroupItem * group = self.groups[section];
 20 
 21     return group.items.count;
 22 
 23 }
 24 
 25  
 26 
 27 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 28 
 29 {
 30 
 31     // 1.创建cell
 32 
 33     iCocosSettingCell *cell =  [iCocosSettingCell cellWithTableView:tableView];
 34 
 35     
 36 
 37     // 取模型
 38 
 39     // 哪一组的模型
 40 
 41     iCocosGroupItem *group = self.groups[indexPath.section];
 42 
 43     
 44 
 45     // 从模型数组数组中取出对应的模型
 46 
 47     iCocosSettingItem *item = group.items[indexPath.row];
 48 
 49     
 50 
 51     // 2.给cell传递模型,给cell的子控件赋值
 52 
 53     cell.item = item;
 54 
 55     
 56 
 57     return cell;
 58 
 59 }
 60 
 61  
 62 
 63 // 返回第section组的头部标题
 64 
 65 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
 66 
 67 {
 68 
 69     // 取出当前是哪一组
 70 
 71     iCocosGroupItem *group = self.groups[section];
 72 
 73     
 74 
 75     return group.headerTitle;
 76 
 77 }
 78 
 79  
 80 
 81 // 返回第section组的尾部标题
 82 
 83 - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
 84 
 85 {
 86 
 87     // 取出当前是哪一组
 88 
 89     iCocosGroupItem *group = self.groups[section];
 90 
 91     
 92 
 93     return group.footerTitle;
 94 
 95 }
 96 
 97  
 98 
 99  
100 
101 #pragma mark - 监听cell点击
102 
103 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
104 
105 {
106 
107     
108 
109     [tableView deselectRowAtIndexPath:indexPath animated:YES];
110 
111     
112 
113     // 取出模型
114 
115     iCocosGroupItem *group = self.groups[indexPath.section];
116 
117     
118 
119     iCocosSettingItem *item = group.items[indexPath.row];
120 
121     
122 
123     // 判断下有木有事情,就判断下block有没有值
124 
125     if (item.operationBlock) {
126 
127         
128 
129         // 执行保存的代码
130 
131         item.operationBlock();
132 
133         
134 
135         return;
136 
137     }
138 
139     
140 
141     if ([item isKindOfClass:[iCocosSettingArrowItem class]]) {
142 
143         iCocosSettingArrowItem *arrowItem = (iCocosSettingArrowItem *)item;
144 
145         
146 
147         
148 
149         if (arrowItem.descVc) {
150 
151             // 创建目的控制器
152 
153             UIViewController *vc = [[arrowItem.descVc alloc] init];
154 
155             
156 
157             vc.navigationItem.title = item.title;
158 
159             
160 
161             // 跳转界面
162 
163             [self.navigationController pushViewController:vc animated:YES];
164 
165         }
166 
167         
168 
169         
170 
171     }
172 
173     
174 
175     
176 
177 }

 

点击对应的行之后就会显示对应的界面:

 

 技术分享

 

以后如果需要拓展任何点击进入里面子控件或者孙子空间我们只需要同上面的操作就可以了,其它操作就是一些对应的数据的修改的优化。

 

 

最后的封装:基类的实现 

 

最后实现一下基类的抽取,以后需要用的话就更简单了,只需要拷贝到项目类似上面的操作直接使用酒可以

声明一个数组的组模型用来给外部使用

 

1 @interface iCocosBaseSettingController : UITableViewController
2 
3 // 总共的组数
4 
5 @property (nonatomic, strong) NSMutableArray *groups;
6 
7 @end
8 
9  

 

 

来看看基类的实现:

 

  1 #import "iCocosBaseSettingController.h"
  2 
  3  
  4 
  5  
  6 
  7 @interface iCocosBaseSettingController ()
  8 
  9  
 10 
 11  
 12 
 13  
 14 
 15 @end
 16 
 17  
 18 
 19 @implementation iCocosBaseSettingController
 20 
 21 - (instancetype)init
 22 
 23 {
 24 
 25     return [self initWithStyle:UITableViewStyleGrouped];
 26 
 27 }
 28 
 29 - (NSMutableArray *)groups
 30 
 31 {
 32 
 33     if (_groups == nil) {
 34 
 35         _groups = [NSMutableArray array];
 36 
 37     }
 38 
 39     return _groups;
 40 
 41 }
 42 
 43  
 44 
 45  
 46 
 47 #pragma mark - 数据源
 48 
 49 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 50 
 51 {
 52 
 53     return self.groups.count;
 54 
 55 }
 56 
 57  
 58 
 59 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 60 
 61 {
 62 
 63     // 取出当前的组模型
 64 
 65     iCocosGroupItem * group = self.groups[section];
 66 
 67     return group.items.count;
 68 
 69 }
 70 
 71  
 72 
 73 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 74 
 75 {
 76 
 77     // 1.创建cell
 78 
 79     iCocosSettingCell *cell =  [iCocosSettingCell cellWithTableView:tableView];
 80 
 81     
 82 
 83     // 取模型
 84 
 85     // 哪一组的模型
 86 
 87     iCocosGroupItem *group = self.groups[indexPath.section];
 88 
 89     
 90 
 91     // 从模型数组数组中取出对应的模型
 92 
 93     iCocosSettingItem *item = group.items[indexPath.row];
 94 
 95     
 96 
 97     // 2.给cell传递模型,给cell的子控件赋值
 98 
 99     cell.item = item;
100 
101     
102 
103     
104 
105     return cell;
106 
107 }
108 
109  
110 
111 // 返回第section组的头部标题
112 
113 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
114 
115 {
116 
117     // 取出当前是哪一组
118 
119     iCocosGroupItem *group = self.groups[section];
120 
121     
122 
123     return group.headerTitle;
124 
125 }
126 
127  
128 
129 // 返回第section组的尾部标题
130 
131 - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
132 
133 {
134 
135     // 取出当前是哪一组
136 
137     iCocosGroupItem *group = self.groups[section];
138 
139     
140 
141     return group.footerTitle;
142 
143 }
144 
145  
146 
147  
148 
149 #pragma mark - 监听cell点击
150 
151 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
152 
153 {
154 
155     
156 
157     [tableView deselectRowAtIndexPath:indexPath animated:YES];
158 
159     
160 
161     // 取出模型
162 
163     iCocosGroupItem *group = self.groups[indexPath.section];
164 
165     
166 
167     iCocosSettingItem *item = group.items[indexPath.row];
168 
169     
170 
171     // 判断下有木有事情,就判断下block有没有值
172 
173     if (item.operationBlock) {
174 
175         
176 
177         // 执行保存的代码
178 
179         item.operationBlock(indexPath);
180 
181         
182 
183         return;
184 
185     }
186 
187     
188 
189     if ([item isKindOfClass:[iCocosSettingArrowItem class]]) {
190 
191         iCocosSettingArrowItem *arrowItem = (iCocosSettingArrowItem *)item;
192 
193         
194 
195         
196 
197         if (arrowItem.descVc) {
198 
199             // 创建目的控制器
200 
201             UIViewController *vc = [[arrowItem.descVc alloc] init];
202 
203             
204 
205             vc.navigationItem.title = item.title;
206 
207             
208 
209             // 跳转界面
210 
211             [self.navigationController pushViewController:vc animated:YES];
212 
213         }
214 
215         
216 
217         
218 
219     }
220 
221     
222 
223     
224 
225 }
226 
227  

 

 

以后使用的话直接将我们创建的类继承子我们基类并且添加对应的行组数据就可以,非常简单:

 

简单使用:

先来看看简单的使用

 

 1 - (void)viewDidLoad {
 2 
 3     [super viewDidLoad];
 4 
 5     
 6 
 7     // 添加第0组
 8 
 9     [self setUpGroup0];
10 
11     
12 
13 }
14 
15  
16 
17 // 添加第0组
18 
19 - (void)setUpGroup0
20 
21 {
22 
23     // 创建行模型
24 
25     // 开奖推送
26 
27     iCocosSettingArrowItem *item = [iCocosSettingArrowItem itemWithImage:nil title:@"开奖推送"];
28 
29     item.descVc = [UIViewController class];
30 
31     
32 
33     // 比分直播
34 
35     iCocosSettingArrowItem *item1 = [iCocosSettingArrowItem itemWithImage:nil title:@"比分直播"];
36 
37     item1.descVc = [iCocosScoreViewController class];
38 
39     
40 
41     // 中奖动画
42 
43     iCocosSettingArrowItem *item2 = [iCocosSettingArrowItem itemWithImage:nil title:@"中奖动画"];
44 
45     // 购彩提醒
46 
47     iCocosSettingArrowItem *item3 = [iCocosSettingArrowItem itemWithImage:nil title:@"购彩提醒"];
48 
49        // Items:存储当前数组有多少行模型
50 
51     // 创建一个组模型,描述第0组
52 
53     iCocosGroupItem *group = [iCocosGroupItem groupWithItems:@[item,item1,item2,item3]];
54 
55  
56 
57     
58 
59     // 添加组模型到groups数组,有多少个组模型就有多少组
60 
61     [self.groups addObject:group];
62 
63 }
64 
65  

技术分享

 

 

比如设置界面:

 

  1 @interface iCocosSettingViewController ()
  2 
  3  
  4 
  5 @end
  6 
  7  
  8 
  9 @implementation iCocosSettingViewController
 10 
 11  
 12 
 13  
 14 
 15 - (void)viewDidLoad {
 16 
 17     [super viewDidLoad];
 18 
 19     
 20 
 21 //    self.navigationItem.title = @"设置";
 22 
 23     // 设置导航条的标题
 24 
 25     self.title = @"设置";
 26 
 27     
 28 
 29     // 添加第0组
 30 
 31     [self setUpGroup0];
 32 
 33     
 34 
 35     // 添加第1组
 36 
 37     [self setUpGroup1];
 38 
 39     
 40 
 41     // 添加第2组
 42 
 43     [self setUpGroup2];
 44 
 45     
 46 
 47 }
 48 
 49 // 当一个对象要销毁的时候,就会调用这个方法
 50 
 51 - (void)dealloc
 52 
 53 {
 54 
 55     NSLog(@"%s",__func__);
 56 
 57 }
 58 
 59  
 60 
 61 // 添加第0组
 62 
 63 - (void)setUpGroup0
 64 
 65 {
 66 
 67     // 创建行模型
 68 
 69     // 使用兑换码
 70 
 71     iCocosSettingArrowItem *RedeemCode = [iCocosSettingArrowItem itemWithImage:[UIImage imageNamed:@"RedeemCode"] title:@"使用兑换码"];
 72 
 73 //    RedeemCode.descVc = [UIViewController class];
 74 
 75     // 使用block的注意点,尽量避免循环引用
 76 
 77     
 78 
 79     // block会把代码块里面的所有强指针强引用
 80 
 81     // 会把当前控制器的对象强引用
 82 
 83     // 解决循环引用,用weak
 84 
 85     
 86 
 87     // 把self强指针转换为弱指针
 88 
 89     // typeof(x) 获取x的类型 iCocosSettingViewController *
 90 
 91  
 92 
 93     __weak typeof(self) weakSelf = self;
 94 
 95     // 在block中最好不要直接访问成员属性
 96 
 97     RedeemCode.operationBlock = ^(NSIndexPath *indexPath){
 98 
 99     
100 
101         UIViewController *vc = [[UIViewController alloc] init];
102 
103         
104 
105         vc.view.backgroundColor = [UIColor redColor];
106 
107         
108 
109         vc.title = @"asldjasd";
110 
111         
112 
113         [weakSelf.navigationController pushViewController:vc animated:YES];
114 
115         
116 
117         // self -> _groups
118 
119         
120 
121         NSLog(@"%@",weakSelf.groups);
122 
123     };
124 
125     
126 
127     // Items:存储当前数组有多少行模型
128 
129     // 创建一个组模型,描述第0组
130 
131     iCocosGroupItem *group = [iCocosGroupItem groupWithItems:@[RedeemCode]];
132 
133    
134 
135     // 设置头部标题
136 
137     group.headerTitle = @"abc";
138 
139     
140 
141     // 添加组模型到groups数组,有多少个组模型就有多少组
142 
143     [self.groups addObject:group];
144 
145 }
146 
147  
148 
149  
150 
151  
152 
153 // 添加第1组
154 
155 - (void)setUpGroup1
156 
157 {
158 
159  
160 
161     // 推送和提醒
162 
163     iCocosSettingArrowItem *push = [iCocosSettingArrowItem itemWithImage:[UIImage imageNamed:@"RedeemCode"] title:@"推送和提醒"];
164 
165     // 设置目的控制器的类名
166 
167     push.descVc = [iCocosPushViewController class];
168 
169     
170 
171     // 使用兑换码
172 
173     iCocosSettingItem *RedeemCode1 = [iCocosSettingSwitchItem itemWithImage:[UIImage imageNamed:@"RedeemCode"] title:@"使用兑换码"];
174 
175     
176 
177     // 使用兑换码
178 
179     iCocosSettingItem *RedeemCode2 = [iCocosSettingSwitchItem itemWithImage:[UIImage imageNamed:@"RedeemCode"] title:@"使用兑换码"];
180 
181     // 使用兑换码
182 
183     iCocosSettingItem *RedeemCode3 = [iCocosSettingSwitchItem itemWithImage:[UIImage imageNamed:@"RedeemCode"] title:@"使用兑换码"];
184 
185     
186 
187      // 描述第一组有多少个行模型,描述第1组
188 
189     NSArray *items = @[push,RedeemCode1,RedeemCode2,RedeemCode3];
190 
191     
192 
193     // 创建组模型
194 
195     iCocosGroupItem *group = [iCocosGroupItem groupWithItems:items];
196 
197     
198 
199     group.headerTitle = @"asd";
200 
201     group.footerTitle = @"asdasdq";
202 
203     
204 
205     // 添加到group总数组
206 
207     [self.groups addObject:group];
208 
209 }
210 
211  
212 
213 // 添加第2组
214 
215 - (void)setUpGroup2
216 
217 {
218 
219     // 使用兑换码
220 
221     iCocosSettingItem *version = [iCocosSettingArrowItem itemWithImage:[UIImage imageNamed:@"RedeemCode"] title:@"检查新版本"];
222 
223     
224 
225     // 保存检查新版本需要做的事情
226 
227     version.operationBlock = ^(NSIndexPath *indexPath){
228 
229          [MBProgressHUD showSuccess:@"没有最新的版本"];
230 
231     };
232 
233     
234 
235     
236 
237     // 使用兑换码
238 
239     iCocosSettingItem *RedeemCode1 = [iCocosSettingArrowItem itemWithImage:[UIImage imageNamed:@"RedeemCode"] title:@"使用兑换码"];
240 
241     
242 
243     // 使用兑换码
244 
245     iCocosSettingItem *RedeemCode2 = [iCocosSettingArrowItem itemWithImage:[UIImage imageNamed:@"RedeemCode"] title:@"使用兑换码"];
246 
247     // 使用兑换码
248 
249     iCocosSettingItem *RedeemCode3 = [iCocosSettingArrowItem itemWithImage:[UIImage imageNamed:@"RedeemCode"] title:@"使用兑换码"];
250 
251     
252 
253     // 描述第一组有多少个行模型,描述第1组
254 
255     NSArray *items = @[version,RedeemCode1,RedeemCode2,RedeemCode3];
256 
257     
258 
259     // 创建组模型
260 
261     iCocosGroupItem *group = [iCocosGroupItem groupWithItems:items];
262 
263     
264 
265     group.footerTitle = @"bcd";
266 
267     
268 
269     // 添加到group总数组
270 
271     [self.groups addObject:group];
272 
273 }
274 
275  

 

 技术分享

 

比分直播界面

 

  1 @interface iCocosScoreViewController ()
  2 
  3  
  4 
  5 @end
  6 
  7  
  8 
  9 @implementation iCocosScoreViewController
 10 
 11  
 12 
 13 - (void)viewDidLoad {
 14 
 15     [super viewDidLoad];
 16 
 17     // Do any additional setup after loading the view.
 18 
 19     // 添加3组
 20 
 21     
 22 
 23     [self setUpGroup0];
 24 
 25     
 26 
 27     [self setUpGroup1];
 28 
 29     
 30 
 31     [self setUpGroup2];
 32 
 33      [self setUpGroup2];
 34 
 35      [self setUpGroup2];
 36 
 37      [self setUpGroup2];
 38 
 39      [self setUpGroup2];
 40 
 41 }
 42 
 43  
 44 
 45 - (void)setUpGroup0
 46 
 47 {
 48 
 49     // 创建行模型
 50 
 51     iCocosSettingSwitchItem *item = [iCocosSettingSwitchItem itemWithImage:nil title:@"关注比赛"];
 52 
 53     
 54 
 55     
 56 
 57     // 创建组模型
 58 
 59     iCocosGroupItem *group = [iCocosGroupItem groupWithItems:@[item]];
 60 
 61     
 62 
 63     group.footerTitle = @"sadsad";
 64 
 65     
 66 
 67     // 添加groups数组
 68 
 69     [self.groups addObject:group];
 70 
 71 }
 72 
 73  
 74 
 75 - (void)setUpGroup1
 76 
 77 {
 78 
 79     // 创建行模型
 80 
 81     iCocosSettingItem *item = [iCocosSettingItem itemWithImage:nil title:@"起始时间"];
 82 
 83     item.subTitle = @"00:00";
 84 
 85     
 86 
 87     // 创建组模型
 88 
 89     iCocosGroupItem *group = [iCocosGroupItem groupWithItems:@[item]];
 90 
 91     
 92 
 93     // 添加groups数组
 94 
 95     [self.groups addObject:group];
 96 
 97 }
 98 
 99 - (void)setUpGroup2
100 
101 {
102 
103     // 创建行模型
104 
105     iCocosSettingItem *item = [iCocosSettingItem itemWithImage:nil title:@"结束时间"];
106 
107     item.subTitle = @"23:59";
108 
109     
110 
111     __weak typeof(self) weakSelf = self;
112 
113     
114 
115     item.operationBlock = ^(NSIndexPath *indexPath){
116 
117         
118 
119         // 获取选中的cell,把键盘添加到cell上面
120 
121         UITableViewCell *cell = [weakSelf.tableView cellForRowAtIndexPath:indexPath];
122 
123         
124 
125         // 弹出键盘
126 
127         UITextField *textField = [[UITextField alloc] init];
128 
129         
130 
131         [textField becomeFirstResponder];
132 
133         
134 
135         
136 
137         [cell addSubview:textField];
138 
139         
140 
141         // 在iOS7之后,只要把textField添加到需要弹出键盘的cell上面,就会自动做好键盘处理
142 
143     
144 
145     };
146 
147     
148 
149     // 创建组模型
150 
151     iCocosGroupItem *group = [iCocosGroupItem groupWithItems:@[item]];
152 
153     
154 
155     // 添加groups数组
156 
157     [self.groups addObject:group];
158 
159 }
160 
161  
162 
163 - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
164 
165 {
166 
167     [self.view endEditing:YES];
168 
169 }
170 
171 @end
172 
173  
174 
175  

技术分享

 

好了终于扯完了,希望对你有用,也方便自己以后复习 

最后附上一份封装好的相关文件下载:http://i.cnblogs.com/Files.aspx

iOS开发——开发必备OC篇&UITableView设置界面完整封装(四)

标签:

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

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