彩票3-0619
CollectinView代码 UICollectionViewController NJSettingCell代码
// NJProductViewController.m // 09-彩票(lottery) #import "NJProductViewController.h" #define NJIdentifier @"COLLECTION" @interface NJProductViewController () @end @implementation NJProductViewController - (id)init { // UICollectionViewLayout // 布局对象决定了将来CollectionView上每一个Cell显示的方式 // 创建一个布局对象 UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; // 设置每一个cell的宽高 (cell在CollectionView中称之为item) layout.itemSize = CGSizeMake(100, 100); // 设置item行与行之间的间隙 layout.minimumLineSpacing = 20; // 设置item列与列之间的间隙 // layout.minimumInteritemSpacing = 30; // 在初始化的时候传入自己创建的布局对象 if (self = [super initWithCollectionViewLayout:layout]) { } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.navigationItem.title = @"产品推荐"; // 告诉系统将来需要创建什么样的cell(在获取cell之前必须先注册一个cell到系统中) [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:NJIdentifier]; } #pragma mark - 数据源方法 // 告诉系统一共有多少组 - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } // 告诉系统第section组有多少行 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return 10; } // 告诉系统indexPath的第Section组的item行显示什么内容 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { // indexPath.section;// 第几组 // indexPath.item;// 第几个 // 从缓存池中获取cell UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NJIdentifier forIndexPath:indexPath]; // if (cell == nil) { // cell = [[UICollectionViewCell alloc] init] // } cell.backgroundColor = [UIColor greenColor]; return cell; } @end
// NJSettingController.m // 09-彩票(lottery)
#import "NJSettingController.h" #import "NJTestViewController.h" #import "NJSettingItem.h" #import "NJSettingGroup.h" #import "NJProductCell.h" #import "NJSettingArrowItem.h" #import "NJSettingSwitchItem.h" #import "MBProgressHUD+NJ.h" #import "NJProductViewController.h" @interface NJSettingController () @property (nonatomic, strong) NSMutableArray *datas; @end @implementation NJSettingController #pragma mark - 懒加载 - (NSMutableArray *)datas { if (_datas == nil) { // 第一组数据 NJSettingItem *item00 = [[NJSettingArrowItem alloc] initWithIcon:@"MorePush" title:@"推送和提醒" destClass:[NJTestViewController class]]; NJSettingItem *item01 = [[NJSettingSwitchItem alloc] initWithIcon:@"MorePush" title:@"摇一摇机选"]; NJSettingGroup *group1 = [[NJSettingGroup alloc] init]; group1.items = @[item00 ,item01]; // 第2组数据 NJSettingItem *item10 = [[NJSettingItem alloc] initWithIcon:@"MorePush" title:@"检查新版本"]; item10.option = ^{ // NSLog(@"发送网络请求检查版本更新"); // 模拟发送网络请求 [MBProgressHUD showMessage:@"正在拼命检查..."]; // 2秒之后删除提示 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [MBProgressHUD hideHUD]; // 提示没有新版本 [MBProgressHUD showSuccess:@"亲~没有新版本"]; }); }; NJSettingItem *item11 = [[NJSettingArrowItem alloc] initWithIcon:@"MorePush" title:@"帮助" destClass:[NJTestViewController class]]; NJSettingItem *item12 = [[NJSettingArrowItem alloc] initWithIcon:@"MorePush" title:@"产品推荐" destClass:[NJProductViewController class]]; NJSettingGroup *group2 = [[NJSettingGroup alloc] init]; group2.items = @[item10, item11, item12]; _datas = [NSMutableArray array]; [_datas addObject:group1]; [_datas addObject:group2]; } return _datas; } - (id)init { return [super initWithStyle:UITableViewStyleGrouped]; } - (id)initWithStyle:(UITableViewStyle)style { return [super initWithStyle:UITableViewStyleGrouped]; } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.datas.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // 先取出对应组的小数组 NJSettingGroup *g = self.datas[section]; // 返回小数组的长度 return g.items.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 1.创建cell NJProductCell *cell = [NJProductCell cellWithTableView:tableView]; // 先取出对应组的组模型 NJSettingGroup *g = self.datas[indexPath.section]; // 从组模型中取出对应行的模型 NJSettingItem *item = g.items[indexPath.row]; cell.item = item; // 3.返回cell return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // 立即取消选中 [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; // 先取出对应组的组模型 NJSettingGroup *g = self.datas[indexPath.section]; // 从组模型中取出对应行的模型 NJSettingItem *item = g.items[indexPath.row]; // 判断block中是否保存了代码 if (item.option != nil) { // 如果保存,就执行block中保存的代码 item.option(); }else if ([item isKindOfClass:[NJSettingArrowItem class]]) { // 创建目标控制并且添加到栈中 NJSettingArrowItem *arrowItem = (NJSettingArrowItem *)item; UIViewController *vc = [[arrowItem.destVC alloc] init]; [self.navigationController pushViewController:vc animated:YES]; } } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { // 先取出对应组的组模型 NJSettingGroup *g = self.datas[section]; return g.headerTitle; } - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section { // 先取出对应组的组模型 NJSettingGroup *g = self.datas[section]; return g.footerTitle; } @end
第三方框架可以解决 kvc 必须要Model中的属性要等于或者多于dictionary
自己字典转模型 如果模型属性不够 就不能用 [self setValuesForKeyWithDictionary]
而是将字典中的有用的值 赋值给 模型
自定义UICollectionViewCell 用xib连线
xib 拖一个UICollectionViewCell 设置identifier
新建类MYUICollectionViewCell
来管理这个cell
注册xib 而不是注册自定义类
这个只能在awakeFromNib里 不能在initWithCoder因为 initWithCoder里系统会将这个设置为NO
---
相同的代码抽取一个父类类
面向借口的开发 只需要alloc] init]即可 里面的不开放
with点击存储数据到preference
控制机器view变成webView
---
------
tableView为Group分组样式后 tableView会自动有一个backgroundView 盖住backgroundColor