一、我只想说封装的思想很重要,MVC的思想也很重要,利用MVC思想搭建一下的界面
- 先说显示出来的cell,有三种(图中的两种,还有一种是最普通的,没有图片的),这种显示不同的cell,交给模型来处理,模型中的数据决定了要显示的样式。
- 但是有考虑到功能的不一样,所以运用了面向对象的思想,同时继承自cell,实现具体到每一种会有单独的样式和功能。
- 不同的样式只要根据类名来判断,展示不同的效果。
- 初始化cell,通过类名来判断是带箭头的cell还是带开关的cell
1 @interface ChaosSettingCell () 2 /** arrow */ 3 @property(nonatomic,strong) UIImageView *arrowView; 4 /** switch */ 5 @property(nonatomic,strong) UISwitch *switchView; 6 @end 7 @implementation ChaosSettingCell 8 9 - (UIImageView *)arrowView 10 { 11 if (_arrowView == nil) { 12 _arrowView = [[UIImageView alloc]initWithImage: [UIImage imageNamed:@"arrow_right"]]; 13 } 14 return _arrowView; 15 } 16 17 - (UISwitch *)switchView 18 { 19 if (_switchView == nil) { 20 _switchView = [[UISwitch alloc]init]; 21 } 22 return _switchView; 23 } 24 25 #pragma mark - 自定义cell内部实现,,返回cell的方法 26 + (ChaosSettingCell *)cellWithTableView:(UITableView *)tableView style:(UITableViewCellStyle)style 27 { 28 static NSString *ID = @"cell"; 29 30 ChaosSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; 31 if (cell == nil) { 32 cell = [[ChaosSettingCell alloc] initWithStyle:style reuseIdentifier:ID]; 33 } 34 return cell; 35 } 36 37 // 重写cell的set方法,给cell赋值 38 - (void)setItem:(ChaosSettingItem *)item 39 { 40 _item = item; 41 42 [self setUpData]; 43 44 [self setUpAccessoryView]; 45 } 46 47 // 给cell绑定数据 48 - (void)setUpData 49 { 50 self.imageView.image = self.item.icon; 51 self.textLabel.text = self.item.title; 52 self.detailTextLabel.text = self.item.subTitle; 53 } 54 // 设置cell的辅助视图 55 - (void)setUpAccessoryView 56 { 57 if ([self.item isKindOfClass:[ChaosSettingItemArrow class]]) { // 箭头 58 59 self.accessoryView = self.arrowView; 60 61 } else if([self.item isKindOfClass:[ChaosSettingItemSwitch class]]){ // 开关 62 63 self.accessoryView = self.switchView; 64 self.selectionStyle = UITableViewCellSelectionStyleNone; 65 66 } else { // 还原 67 68 self.accessoryView = nil; 69 self.selectionStyle = UITableViewCellSelectionStyleDefault; 70 } 71 } 72 73 @end
- 通过设置不同的模型,完全实现了展示的效果
1 #import "ChaosSettingViewController.h" 2 #import "ChaosHelpViewController.h" 3 #import "ChaosSettingGroup.h" 4 5 #import "ChaosPushViewController.h" 6 #import "ChaosHelpViewController.h" 7 8 #import "ChaosBlurView.h" 9 #import "MBProgressHUD+XMG.h" 10 11 12 @interface ChaosSettingViewController () 13 14 @end 15 16 @implementation ChaosSettingViewController 17 18 19 - (instancetype)init 20 { 21 return [super initWithStyle:UITableViewStyleGrouped]; 22 } 23 24 - (void)viewDidLoad { 25 [super viewDidLoad]; 26 27 self.title = @"设置"; 28 self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"常见问题" style:UIBarButtonItemStyleBordered target:self action:@selector(help)]; 29 30 [self setUpGroup0]; 31 [self setUpGroup1]; 32 [self setUpGroup2]; 33 } 34 35 - (void)help 36 { 37 ChaosHelpViewController *helpVC = [[ChaosHelpViewController alloc] init]; 38 39 helpVC.title = @"帮助"; 40 41 [self.navigationController pushViewController:helpVC animated:YES]; 42 } 43 44 - (void)setUpGroup0 45 { 46 ChaosSettingItemArrow *item = [ChaosSettingItemArrow itemWithImage:[UIImage imageNamed:@"RedeemCode"] title:@"使用兑换码"]; 47 item.pushVCName = [UITableViewController class]; 48 NSArray *items = @[item]; 49 ChaosSettingGroup *group = [ChaosSettingGroup groupWithHeader:nil footer:nil items:items]; 50 [self.sections addObject:group]; 51 } 52 - (void)setUpGroup1 53 { 54 ChaosSettingItemArrow *item = [ChaosSettingItemArrow itemWithImage:[UIImage imageNamed:@"MorePush"] title:@"推送和提醒"]; 55 56 item.pushVCName = [ChaosPushViewController class]; 57 58 ChaosSettingItemSwitch *item1 = [ChaosSettingItemSwitch itemWithImage:[UIImage imageNamed:@"more_homeshake"] title:@"摇一摇机选"]; 59 ChaosSettingItemSwitch *item2 = [ChaosSettingItemSwitch itemWithImage:[UIImage imageNamed:@"sound_Effect"] title:@"声音效果"]; 60 ChaosSettingItemSwitch *item3 = [ChaosSettingItemSwitch itemWithImage:[UIImage imageNamed:@"More_LotteryRecommend"] title:@"采购小助手"]; 61 NSArray *items = @[item,item1,item2,item3]; 62 ChaosSettingGroup *group = [ChaosSettingGroup groupWithHeader:nil footer:nil items:items]; 63 [self.sections addObject:group]; 64 } 65 - (void)setUpGroup2 66 { 67 ChaosSettingItemArrow *item = [ChaosSettingItemArrow itemWithImage:[UIImage imageNamed:@"MoreUpdate"] title:@"检查新版本"]; 68 item.itemOpertion = ^(NSIndexPath *indexPath){ 69 // 高斯模糊框架的应用 70 ChaosBlurView *blurView = [[ChaosBlurView alloc] initWithFrame:ChaosScreenBounds]; 71 [ChaosKeyWindow addSubview:blurView]; 72 73 [MBProgressHUD showSuccess:@"没有新版本可更新"]; 74 75 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 76 [blurView removeFromSuperview]; 77 }); 78 79 }; 80 ChaosSettingItemArrow *item1 = [ChaosSettingItemArrow itemWithImage:[UIImage imageNamed:@"MoreShare"] title:@"分享"]; 81 ChaosSettingItemArrow *item2 = [ChaosSettingItemArrow itemWithImage:[UIImage imageNamed:@"MoreNetease"] title:@"产品推荐"]; 82 ChaosSettingItemArrow *item3 = [ChaosSettingItemArrow itemWithImage:[UIImage imageNamed:@"MoreAbout"] title:@"关于"]; 83 NSArray *items = @[item,item1,item2,item3]; 84 ChaosSettingGroup *group = [ChaosSettingGroup groupWithHeader:nil footer:nil items:items]; 85 [self.sections addObject:group]; 86 } 87 88 @end
- 模型中的
/** itemblock */
@property(nonatomic,strong) void(^itemOpertion)(NSIndexPath *indexPath); 这个属性用来保存代码段。项目中是点击了相应的cell后需要做的事情保存到了block中