在APP里面经常可以看到如上的界面,自己写了一遍之后,感觉在以后写APP的过程中,改一下Model,然后直接粘贴复制就可以了。
从图中可以直观的看出,这就是一个tableView的分组样式。每一个cell的左边是一个imageView,接着是一个标签,最后就是一个accessoryView。主要是根据MVC的思想,该分离的分离,该自定义的自定义就行啦。
下面给出我写的代码,下次写别的程序的时候根据相应的业务逻辑去修改即可,就不用自己费尽再去敲了。
控制器(Controller):
// // LrdSettingTableViewController.m // Lottery // // Created by 键盘上的舞者 on 15/9/11. // Copyright (c) 2015年 键盘上的舞者. All rights reserved. // #import "LrdSettingTableViewController.h" #import "LrdSettingItem.h" #import "LrdSettingGroup.h" #import "LrdSettingCell.h" #import "LrdSettingSwitchItem.h" #import "LrdSettingArrowItem.h" #import "LrdTestController.h" #import "MBProgressHUD+MJ.h" @interface LrdSettingTableViewController() @property(nonatomic,strong) NSMutableArray *dataArray; @end @implementation LrdSettingTableViewController -(void)viewDidLoad { [super viewDidLoad]; [self dataArray]; } -(NSMutableArray *)dataArray { if(_dataArray==nil){ _dataArray=[NSMutableArray array]; //第一组 LrdSettingGroup *group0=[[LrdSettingGroup alloc] init]; LrdSettingArrowItem *noticePush=[LrdSettingArrowItem initWithIcon:@"MorePush" title:@"推送和提醒"]; noticePush.destVcClass=[LrdTestController class]; LrdSettingItem *handShake=[LrdSettingSwitchItem initWithIcon:@"handShake" title:@"摇一摇机选"]; LrdSettingItem *sound=[LrdSettingSwitchItem initWithIcon:@"sound_Effect" title:@"声音效果"]; group0.items=@[noticePush,handShake,sound]; //第二组 LrdSettingGroup *group1=[[LrdSettingGroup alloc] init]; LrdSettingItem *check=[LrdSettingArrowItem initWithIcon:@"MoreUpdate" title:@"检测更新"]; check.operation=^{ //设置蒙版 [MBProgressHUD showMessage:@"正在检测更新...."]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ //隐藏蒙版 [MBProgressHUD hideHUD]; //提示用户 UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"发现新版本" message:nil delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"立即更新", nil]; [alert show]; }); }; LrdSettingItem *help=[LrdSettingArrowItem initWithIcon:@"MoreHelp" title:@"帮助"]; LrdSettingItem *share=[LrdSettingArrowItem initWithIcon:@"MoreShare" title:@"分享"]; LrdSettingItem *lookNotice=[LrdSettingArrowItem initWithIcon:@"MoreMessage" title:@"查看消息"]; LrdSettingItem *productRecommend=[LrdSettingArrowItem initWithIcon:@"MoreNetease" title:@"产品推荐"]; LrdSettingItem *about=[LrdSettingArrowItem initWithIcon:@"MoreAbout" title:@"关于"]; group1.items=@[check,help,share,lookNotice,productRecommend,about]; //添加至dataArray [_dataArray addObject:group0]; [_dataArray addObject:group1]; } return _dataArray; } //重写Init方法,直接返回group样式 -(id)init { return [super initWithStyle:UITableViewStyleGrouped]; } //每个分组中有多少行 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { LrdSettingGroup *group=self.dataArray[section]; return group.items.count; } //有多少个分组 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.dataArray.count; } //设置cell -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { LrdSettingCell *cell=[LrdSettingCell cellWithTableView:tableView]; //设置每个cell的信息 //取出模型 LrdSettingGroup *group=self.dataArray[indexPath.section]; LrdSettingItem *item=group.items[indexPath.row]; cell.item=item; return cell; } //控制器的跳转 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { //取出模型 LrdSettingGroup *group=self.dataArray[indexPath.section]; LrdSettingItem *item=group.items[indexPath.row]; //如果存在Block,就执行它 if (item.operation) { item.operation(); return ; } if([item isKindOfClass:[LrdSettingArrowItem class]]){ LrdSettingArrowItem *arrItem=(LrdSettingArrowItem *)item; //右侧是箭头就实现跳转 UIViewController *control=[[arrItem.destVcClass alloc] init]; [self.navigationController pushViewController:control animated:YES]; } } @end
模型(Model):
// // LrdSettingItem.h // Lottery // // Created by 键盘上的舞者 on 15/9/11. // Copyright (c) 2015年 键盘上的舞者. All rights reserved. // #import <Foundation/Foundation.h> typedef void(^LrdSettingItemOperation)(); @interface LrdSettingItem : NSObject @property(nonatomic,copy) NSString *icon; @property(nonatomic,copy) NSString *title; @property(nonatomic,copy) LrdSettingItemOperation operation; +(instancetype) initWithIcon:(NSString *) icon title:(NSString *) title; @end
// // LrdSettingItem.m // Lottery // // Created by 键盘上的舞者 on 15/9/11. // Copyright (c) 2015年 键盘上的舞者. All rights reserved. // #import "LrdSettingItem.h" @implementation LrdSettingItem +(instancetype)initWithIcon:(NSString *)icon title:(NSString *)title { //注意这里应该使用self,子类 LrdSettingItem *item=[[self alloc] init]; item.icon=icon; item.title=title; return item; } @end
// // LrdSettingGroup.h // Lottery // // Created by 键盘上的舞者 on 15/9/11. // Copyright (c) 2015年 键盘上的舞者. All rights reserved. // #import <Foundation/Foundation.h> @interface LrdSettingGroup : NSObject @property(nonatomic,strong) NSArray *items; @property(nonatomic,copy) NSString *header; @property(nonatomic,copy) NSString *footer; @end
视图(View),自定义的cell
// // LrdSettingCell.m // Lottery // // Created by 键盘上的舞者 on 15/9/11. // Copyright (c) 2015年 键盘上的舞者. All rights reserved. // #import "LrdSettingCell.h" #import "LrdSettingItem.h" #import "LrdSettingArrowItem.h" #import "LrdSettingSwitchItem.h" @interface LrdSettingCell() @property(nonatomic,strong) UISwitch *switchView; @property(nonatomic,strong) UIImageView *imgView; @end @implementation LrdSettingCell //懒加载,减小内存开支 -(UISwitch *)switchView { if(_switchView==nil){ _switchView=[[UISwitch alloc] init]; } return _switchView; } -(UIImageView *)imgView { if(_imgView==nil){ _imgView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"CellArrow"]]; } return _imgView; } -(void)setItem:(LrdSettingItem *)item { _item=item; self.imageView.image=[UIImage imageNamed:item.icon]; self.textLabel.text=item.title; //设置右边的accesstory if([item isKindOfClass:[LrdSettingArrowItem class]]){ self.accessoryView=self.imgView; self.selectionStyle=UITableViewCellSelectionStyleDefault; }else if ([item isKindOfClass:[LrdSettingSwitchItem class]]){ self.accessoryView=self.switchView; self.selectionStyle=UITableViewCellSelectionStyleNone; }else{ self.accessoryView=nil; self.selectionStyle=UITableViewCellSelectionStyleNone; } } +(instancetype)cellWithTableView:(UITableView *)tableView { static NSString *ID=@"cell"; LrdSettingCell *cell=[tableView dequeueReusableCellWithIdentifier:ID]; if(cell==nil){ cell=[[LrdSettingCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; } return cell; } @end
// // LrdSettingCell.h // Lottery // // Created by 键盘上的舞者 on 15/9/11. // Copyright (c) 2015年 键盘上的舞者. All rights reserved. // #import <UIKit/UIKit.h> @class LrdSettingItem; @interface LrdSettingCell : UITableViewCell @property(nonatomic,strong) LrdSettingItem *item; +(instancetype) cellWithTableView:(UITableView *)tableView; @end