IOS彩票第二天设置界面(2)
*********代码的抽取ILBaseTableViewController.h
#import <UIKit/UIKit.h> @interface ILBaseTableViewController : UITableViewController @property (nonatomic, strong) NSMutableArray *dataList; @end
**********ILBaseTableViewController.m
#import "ILBaseTableViewController.h" #import "ILSettingCell.h" #import "ILSettingItem.h" #import "ILSettingArrowItem.h" #import "ILSettingSwitchItem.h" #import "ILSettingGroup.h" @interface ILBaseTableViewController () @end @implementation ILBaseTableViewController - (NSMutableArray *)dataList { if (_dataList == nil) { _dataList = [NSMutableArray array]; } return _dataList; } // 初始化方法 - (id)init { return [super initWithStyle:UITableViewStyleGrouped]; } - (void)viewDidLoad { [super viewDidLoad]; } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.dataList.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. ILSettingGroup *group = self.dataList[section]; return group.items.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 创建cell ILSettingCell *cell = [[ILSettingCell class] cellWithTableView:tableView]; // 取出模型 ILSettingGroup *group = self.dataList[indexPath.section]; ILSettingItem *item = group.items[indexPath.row]; // 传递模型 cell.item = item; return cell; } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { ILSettingGroup *group = self.dataList[section]; return group.header; } - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section { ILSettingGroup *group = self.dataList[section]; return group.footer; } #warning 点击某一行cell - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // 取消选中 [tableView deselectRowAtIndexPath:indexPath animated:YES]; // 取出模型 ILSettingGroup *group = self.dataList[indexPath.section]; ILSettingItem *item = group.items[indexPath.row]; // 执行操作 if (item.option) { item.option(); return; } if ([item isKindOfClass:[ILSettingArrowItem class]]) { // 需要跳转控制器 ILSettingArrowItem *arrowItem = (ILSettingArrowItem *)item; // 创建跳转的控制器 if (arrowItem.destVcClass) { UIViewController *vc = [[arrowItem.destVcClass alloc] init]; vc.title = item.title; [self.navigationController pushViewController:vc animated:YES]; } } } @end
***** ILSettingTableViewController.h
#import <UIKit/UIKit.h> #import "ILBaseTableViewController.h" @interface ILSettingTableViewController : ILBaseTableViewController @end
***** ILSettingTableViewController.m
#import "ILSettingTableViewController.h" #import "ILSettingCell.h" #import "ILSettingItem.h" #import "ILSettingArrowItem.h" #import "ILSettingSwitchItem.h" #import "ILSettingGroup.h" #import "ILTestViewController.h" #import "MBProgressHUD+MJ.h" #import "ILProductViewController.h" #import "ILPushNoticeController.h" @interface ILSettingTableViewController () @end @implementation ILSettingTableViewController - (void)viewDidLoad { [super viewDidLoad]; // 0组 [self addGroup0]; // 1组 [self addGroup1]; } - (void)addGroup1 { // 1组 ILSettingItem *newVersion = [ILSettingArrowItem itemWithIcon:@"MoreUpdate" title:@"检查新版本"]; // 保存了一段检查更新的功能 newVersion.option = ^{ // 1.显示蒙板 [MBProgressHUD showMessage:@"正在检查ing......."]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ // 2.隐藏蒙板 [MBProgressHUD hideHUD]; // 3.提示用户 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"有更新版本" message:nil delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"立即更新", nil]; // 显示UIAlertView [alert show]; }); }; ILSettingItem *help = [ILSettingArrowItem itemWithIcon:@"MoreHelp" title:@"帮助" destVcClass:[ILTestViewController class]]; ILSettingItem *MoreShare = [ILSettingArrowItem itemWithIcon:@"MoreShare" title:@"分享" destVcClass:[ILTestViewController class]]; ILSettingItem *MoreMessage = [ILSettingArrowItem itemWithIcon:@"MoreMessage" title:@"查看消息" destVcClass:[ILTestViewController class]]; ILSettingItem *MoreNetease = [ILSettingArrowItem itemWithIcon:@"MoreNetease" title:@"产品推荐" destVcClass:[ILProductViewController class]]; ILSettingItem *MoreAbout = [ILSettingArrowItem itemWithIcon:@"MoreAbout" title:@"关于" destVcClass:[ILTestViewController class]]; ILSettingGroup *group1 = [[ILSettingGroup alloc] init]; group1.header = @"帮助"; group1.items = @[newVersion,help,MoreShare,MoreMessage,MoreNetease,MoreAbout]; [self.dataList addObject:group1]; } - (void)addGroup0 { // 0组 ILSettingArrowItem *pushNotice = [ILSettingArrowItem itemWithIcon:@"MorePush" title:@"推送和提醒" destVcClass:[ILPushNoticeController class]]; ILSettingItem *yaoyiyao = [ILSettingSwitchItem itemWithIcon:@"handShake" title:@"摇一摇机选"]; ILSettingItem *sound = [ILSettingSwitchItem itemWithIcon:@"sound_Effect" title:@"声音效果"]; ILSettingGroup *group0 = [[ILSettingGroup alloc] init]; group0.items = @[pushNotice,yaoyiyao,sound]; [self.dataList addObject:group0]; } @end
*****ILSettingItem.h
#import <Foundation/Foundation.h> typedef void(^ILSettingItemOption)(); @interface ILSettingItem : NSObject @property (nonatomic, copy) NSString *title; @property (nonatomic, copy) NSString *icon; // 保存一段功能,在恰当的时候调用 @property (nonatomic, copy) ILSettingItemOption option; + (instancetype)itemWithIcon:(NSString *)icon title:(NSString *)title; @end
*****ILSettingItem.m
#import <Foundation/Foundation.h> typedef void(^ILSettingItemOption)(); @interface ILSettingItem : NSObject @property (nonatomic, copy) NSString *title; @property (nonatomic, copy) NSString *icon; // 保存一段功能,在恰当的时候调用 @property (nonatomic, copy) ILSettingItemOption option; + (instancetype)itemWithIcon:(NSString *)icon title:(NSString *)title; @end
****ILSettingLabelItem .h
#import "ILSettingItem.h" @interface ILSettingLabelItem : ILSettingItem // label显示什么内容 @property (nonatomic, copy) NSString *text; @end
***ILSettingLabelItem .m
#import "ILSettingLabelItem.h" #import "ILSaveTool.h" @implementation ILSettingLabelItem - (void)setText:(NSString *)text { _text = text; [ILSaveTool setObject:text forKey:self.title]; } - (void)setTitle:(NSString *)title { [super setTitle:title]; _text = [ILSaveTool objectForKey:self.title]; } @end
*****view ILSettingCell.h
#import <UIKit/UIKit.h> @class ILSettingItem; @interface ILSettingCell : UITableViewCell @property (nonatomic, strong) ILSettingItem *item; + (instancetype)cellWithTableView:(UITableView *)tableView; @end
*****view ILSettingCell.h
#import "ILSettingCell.h" #import "ILSettingItem.h" #import "ILSettingArrowItem.h" #import "ILSettingSwitchItem.h" #import "ILSettingLabelItem.h" @interface ILSettingCell() @property (nonatomic, strong) UIImageView *imgView; @property (nonatomic, strong) UISwitch *switchView; @property (nonatomic, strong) UILabel *labelView; @end @implementation ILSettingCell - (UILabel *)labelView { if (_labelView == nil) { _labelView = [[UILabel alloc] init]; _labelView.bounds = CGRectMake(0, 0, 100, 44); _labelView.textColor = [UIColor redColor]; _labelView.textAlignment = NSTextAlignmentRight; } return _labelView; } - (UIImageView *)imgView { if (_imgView == nil) { _imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"CellArrow"]]; } return _imgView; } - (UISwitch *)switchView { if (_switchView == nil) { _switchView = [[UISwitch alloc] init]; } return _switchView; } - (void)setItem:(ILSettingItem *)item { _item = item; // 1.设置cell的子控件的数据 [self setUpData]; // 2.设置右边视图 [self setUpAccessoryView]; } // 设置cell的子控件的数据 - (void)setUpData { if (_item.icon.length) { self.imageView.image = [UIImage imageNamed:_item.icon]; } self.textLabel.text = _item.title; } // 设置右边视图 - (void)setUpAccessoryView { if ([_item isKindOfClass:[ILSettingArrowItem class]]) { // 箭头 self.accessoryView = self.imgView; self.selectionStyle = UITableViewCellSelectionStyleDefault; }else if ([_item isKindOfClass:[ILSettingSwitchItem class]]){ // Switch self.accessoryView = self.switchView; self.selectionStyle = UITableViewCellSelectionStyleNone; }else if ([_item isKindOfClass:[ILSettingLabelItem class]]){ //lable self.accessoryView = self.labelView; ILSettingLabelItem *labelItem = (ILSettingLabelItem *)_item; self.labelView.text = labelItem.text; self.selectionStyle = UITableViewCellSelectionStyleDefault; }else{ self.accessoryView = nil; self.selectionStyle = UITableViewCellSelectionStyleDefault; } } + (instancetype)cellWithTableView:(UITableView *)tableView { static NSString *ID = @"cell"; ILSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:ID ]; if (cell == nil) { cell = [[ILSettingCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; } return cell; } @end
***ILScoreNoticeViewController.m
#import "ILScoreNoticeViewController.h" #import "ILSettingSwitchItem.h" #import "ILSettingGroup.h" #import "ILSettingLabelItem.h" #import "ILSaveTool.h" @interface ILScoreNoticeViewController () @property (nonatomic, strong) ILSettingLabelItem *start; @end @implementation ILScoreNoticeViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // 0组 [self addGroup0]; // 1组 [self addGroup1]; // 2组 [self addGroup2]; } - (void)addGroup0 { ILSettingSwitchItem *notice = [ILSettingSwitchItem itemWithIcon:nil title:@"提醒我关注比赛"]; ILSettingGroup *group = [[ILSettingGroup alloc] init]; group.items = @[notice]; group.footer = @"asdsad"; [self.dataList addObject:group]; } - (void)addGroup1 { ILSettingLabelItem *start = [ILSettingLabelItem itemWithIcon:nil title:@"开始时间"]; _start = start; if (!start.text.length) { start.text = @"00:00"; } start.option = ^{ UITextField *textField = [[UITextField alloc] init]; UIDatePicker *datePicker = [[UIDatePicker alloc] init]; // 设置模式 datePicker.datePickerMode = UIDatePickerModeTime; // 设置地区 datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh"]; // 创建日期格式对象 NSDateFormatter *dateF = [[NSDateFormatter alloc] init]; dateF.dateFormat = @"HH:mm"; // 设置日期 datePicker.date = [dateF dateFromString:@"00:00"]; // 监听UIDatePicker [datePicker addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged]; // 设置键盘 textField.inputView = datePicker; [textField becomeFirstResponder]; [self.view addSubview:textField]; }; ILSettingGroup *group = [[ILSettingGroup alloc] init]; group.items = @[start]; group.header = @"213214234324"; [self.dataList addObject:group]; } - (void)valueChange:(UIDatePicker *)datePicker { // 创建日期格式对象 NSDateFormatter *dateF = [[NSDateFormatter alloc] init]; dateF.dateFormat = @"HH:mm"; _start.text = [dateF stringFromDate:datePicker.date]; //设置数据 刷新 [self.tableView reloadData]; } - (void)addGroup2 { ILSettingLabelItem *stop = [ILSettingLabelItem itemWithIcon:nil title:@"结束时间"]; stop.text = @"00:00"; ILSettingGroup *group = [[ILSettingGroup alloc] init]; group.items = @[stop]; //加入组 [self.dataList addObject:group]; //把组加入集合 } @end
***ILScoreNoticeViewController.h
#import "ILBaseTableViewController.h" @interface ILScoreNoticeViewController : ILBaseTableViewController @end
***ILPushNoticeController .h
#import <UIKit/UIKit.h> #import "ILBaseTableViewController.h" @interface ILPushNoticeController : ILBaseTableViewController @end
***ILPushNoticeController .m
#import "ILPushNoticeController.h" #import "ILSettingCell.h" #import "ILSettingItem.h" #import "ILSettingArrowItem.h" #import "ILSettingSwitchItem.h" #import "ILSettingGroup.h" #import "ILScoreNoticeViewController.h" @interface ILPushNoticeController () @property (nonatomic, strong) NSMutableArray *dataList; @end @implementation ILPushNoticeController - (void)viewDidLoad { [super viewDidLoad]; // 0组 [self addGroup0]; } - (void)addGroup0 { // 0组 ILSettingArrowItem *push = [ILSettingArrowItem itemWithIcon:nil title:@"开奖号码推送" destVcClass:nil]; ILSettingItem *anim = [ILSettingArrowItem itemWithIcon:nil title:@"中奖动画"]; ILSettingItem *score = [ILSettingArrowItem itemWithIcon:nil title:@"比分直播" destVcClass:[ILScoreNoticeViewController class]]; ILSettingItem *timer = [ILSettingArrowItem itemWithIcon:nil title:@"购彩定时提醒"]; ILSettingGroup *group0 = [[ILSettingGroup alloc] init]; group0.items = @[push,anim,score,timer]; [self.dataList addObject:group0]; } @end
****ILSettingArrowItem.m
#import "ILSettingArrowItem.h" @implementation ILSettingArrowItem + (instancetype)itemWithIcon:(NSString *)icon title:(NSString *)title destVcClass:(Class)destVcClass { ILSettingArrowItem *item = [super itemWithIcon:icon title:title]; item.destVcClass = destVcClass; return item; } @end
*****ILProductViewController.h
#import <UIKit/UIKit.h> @interface ILProductViewController : UICollectionViewController @end
*****ILProductViewController.m
#import "ILProductViewController.h" #import "ILProduct.h" #import "ILProductCell.h" @interface ILProductViewController () @property (nonatomic, strong) NSMutableArray *products; @end @implementation ILProductViewController - (NSMutableArray *)products { if (_products == nil) { _products = [NSMutableArray array]; NSString *fileName = [[NSBundle mainBundle] pathForResource:@"products.json" ofType:nil]; NSData *data = [NSData dataWithContentsOfFile:fileName]; NSArray *jsonArr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; for (NSDictionary *dict in jsonArr) { ILProduct *product = [ILProduct productWithDict:dict]; [_products addObject:product]; } } return _products; } static NSString *ID = @"product"; /* 使用UICollectionView 第一步:必须有布局 第二部:cell必须自己注册 */ - (id)init { // 创建流水布局 UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; // 每一个cell的尺寸 layout.itemSize = CGSizeMake(80, 80); // 垂直间距 layout.minimumLineSpacing = 10; // 水平间距 layout.minimumInteritemSpacing = 0; // 内边距 layout.sectionInset = UIEdgeInsetsMake(10, 0, 0, 0); return [super initWithCollectionViewLayout:layout]; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // 注册UICollectionViewCell,如果没有从缓存池找到,就会自动帮我们创建UICollectionViewCell UINib *xib = [UINib nibWithNibName:@"ILProductCell" bundle:nil]; [self.collectionView registerNib:xib forCellWithReuseIdentifier:ID]; // [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:ID]; self.collectionView.backgroundColor = [UIColor whiteColor]; } // 数据源 - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.products.count; } // 返回每一个cell长什么样 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { ILProductCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath]; // 获取模型 ILProduct *p = self.products[indexPath.item]; cell.product = p; return cell; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { ILProduct *p = self.products[indexPath.item]; NSLog(@"点击了---%@",p.title); } @end
******ILProduct.h
#import "ILProduct.h" @implementation ILProduct + (instancetype)productWithDict:(NSDictionary *)dict{ ILProduct *product = [[ILProduct alloc] init]; product.title = dict[@"title"]; product.icon = dict[@"icon"]; product.url = dict[@"url"]; product.customUrl = dict[@"customUrl"]; product.ID = dict[@"id"]; return product; } - (void)setIcon:(NSString *)icon { _icon = [icon stringByReplacingOccurrencesOfString:@"@2x.png" withString:@""]; } @end
******ILProduct.m
#import <Foundation/Foundation.h> /* { "title": "网易电影票", "id": "com.netease.movie", "url": "http://itunes.apple.com/app/id583784224?mt=8", "icon": "movie@2x.png", "customUrl": "movieticket163" }, */ @interface ILProduct : NSObject @property (nonatomic, copy) NSString *title; @property (nonatomic, copy) NSString *ID; @property (nonatomic, copy) NSString *url; @property (nonatomic, copy) NSString *icon; @property (nonatomic, copy) NSString *customUrl; + (instancetype)productWithDict:(NSDictionary *)dict; @end
没有失败, 只有放弃...