//
// MeViewController.m
// 01-BuDeJie
//
// Created by 苗冬 on 2017/3/15.
// Copyright © 2017年 苗冬. All rights reserved.
//
#import "MeViewController.h"
#import "SettingViewController.h"
#import "MeSquareCollectionViewCell.h"
#import "MeSquareModel.h"
#import <AFNetworking/AFNetworking.h>
#import <MJExtension/MJExtension.h>
#import <SVProgressHUD.h>
#import <SafariServices/SafariServices.h> //iOS9新出的safair框架
static NSString *const reuseIdentifier = @"reuseIdentifier" ;
//总列数:
static NSInteger columns = 4 ;
//间距线:
static CGFloat margin = 1 ;
//cell的高宽
#define itemWH (screenW - (columns - 1) * margin) / columns
@interface MeViewController () <UICollectionViewDataSource , UICollectionViewDelegate/* , SFSafariViewControllerDelegate*/>
@property (nonatomic , strong) NSMutableArray *dataSourceArray ;
@property (nonatomic , weak) UICollectionView *collectionView ;
@end
@implementation MeViewController
- (void)viewDidLoad {
[super viewDidLoad];
//设置导航条:
[self setupNavigationBar] ;
//设置tableView的底部视图:
[self setupFooterView] ;
//网络请求:
[self loadData] ;
//处理cell的间距(默认tableView就有额外的头部和尾部的间距):
self.tableView.sectionHeaderHeight = 0 ;
self.tableView.sectionFooterHeight = 10 ;
//内容视图向上偏移25个单位,保证每一个cell都有10个间距的间距:
self.tableView.contentInset = UIEdgeInsetsMake(-25, 0, 0, 0) ;
}
#pragma mark - 设置导航条
- (void)setupNavigationBar {
//夜间模式按钮:
UIBarButtonItem *nightItem = [UIBarButtonItem itemWithImage:[UIImage imageNamed:@"mine-moon-icon"] andSelectedImage:[UIImage imageNamed:@"mine-sun-icon-click"] addTarget:self action:@selector(nightItemAction:)] ;
//设置按钮:
UIBarButtonItem *settingItem = [UIBarButtonItem itemWithImage:[UIImage imageNamed:@"mine-setting-icon"] andHighLightedImage:[UIImage imageNamed:@"mine-setting-icon-click"] addTarget:self action:@selector(settingItemAction)] ;
self.navigationItem.rightBarButtonItems = @[settingItem , nightItem] ;
//titleView:
self.navigationItem.title = @"我的" ;
}
#pragma mark - 点击夜间模式按钮
- (void)nightItemAction:(UIButton *)selectedButton {
selectedButton.selected = !selectedButton.selected ;
}
#pragma mark - 设置底部视图
- (void)setupFooterView {
/*
初始化一个CollectionView的注意事项:
1.设置一个流水布局 ;
2.cell必须要注册 ;
3.cell必须要自定义 ;
*/
// 1.创建布局:
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init] ;
flowLayout.minimumLineSpacing = margin ;
flowLayout.minimumInteritemSpacing = margin ;
//设置cell的尺寸:
flowLayout.itemSize = CGSizeMake(itemWH, itemWH) ;
// 2.创建collectionView:
//在tableView的底部视图,顶部视图内,宽度,位置点是不用管理的!
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 300) collectionViewLayout:flowLayout] ;
// 3.将 collectionView 放到tableView 的footer上:
self.tableView.tableFooterView = collectionView ;
collectionView.backgroundColor = self.tableView.backgroundColor ;
//让collectionView展示一个一个的小格子,得遵守数据源协议:
collectionView.dataSource = self ;
collectionView.delegate = self ;
//collectionView禁止滚动:
collectionView.scrollEnabled = NO ;
//注册cell:
[collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([MeSquareCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:reuseIdentifier] ;
_collectionView = collectionView ;
}
#pragma mark - 网络请求
- (void)loadData {
NSMutableDictionary *parameters = [NSMutableDictionary dictionary] ;
//拼接请求参数:
parameters[@"字段"] = @"内容" ;
parameters[@"字段"] = @"内容" ;
parameters[@"字段"] = @"内容" ;
parameters[@"字段"] = @"内容" ;
//创建会话管理者并发送请求url:
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager] ;
[manager GET:@"接口数据" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable responseObject) {
[SVProgressHUD dismiss] ;
//要的是square_list这个字段 , 把这个字段字典转模型:
NSArray *dictArray = responseObject[@"square_list"] ;
//接下来创建模型数组,把字典数组dictArray添加到模型数组里:
_dataSourceArray = [MeSquareModel mj_objectArrayWithKeyValuesArray:dictArray] ;
//处理数据:判断是否多余或者少数量为4的倍数:
[self resolveDataSource] ;
//有了数据之后要重新设置collectionView的高度(根据内容自适应高度):
//collectionView.height = row * itemH ;
//Rows = (conut - 1) / columns + 1 ;
NSInteger count = _dataSourceArray.count ;
NSInteger rows = (count - 1) / columns + 1 ;
//collectionView高度动态化:
self.collectionView.height = rows * itemWH ;
//tableView的滚动范围:
//tableView的滚动范围不用人为管理 , 是系统自动会根据内容去计算:
//重新设置collectionView为tableView的footerView:
//我的collectionView的高度一开始设置了一个300的高度,然后tableView就一直认为300的高度是tableView的底部视图(即:collectionView)的高度 , tableView并不知道collectionView的高度根据内容自适应更新了!所以我在这里重新设置collectionView为tableView的底部视图让tableView也更新一下,根据最新的底部视图高度更新一下!
self.tableView.tableFooterView = self.collectionView ;
//有了数据之后,刷新表格:
[self.collectionView reloadData] ;
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[SVProgressHUD showErrorWithStatus:@"网络请求失败/(ㄒoㄒ)/~~"] ;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[SVProgressHUD dismiss] ;
});
}] ;
}
#pragma mark - 处理请求完成的数据
- (void)resolveDataSource {
//判断缺几个?!
//3 % 4 = 3 ---> columns - 3 = 1 ;
NSInteger count = _dataSourceArray.count ;
NSInteger extra = count % columns ;
if (extra) {
extra = columns - extra ;
for (int i = 0; i < extra; i++) {
MeSquareModel *squareModel = [[MeSquareModel alloc] init] ;
[_dataSourceArray addObject:squareModel] ;
}
}
}
#pragma mark - 点击设置按钮
- (void)settingItemAction {
//跳转到设置界面:
SettingViewController *settingVC = [[SettingViewController alloc] init] ;
//这个方法必须放在push之前才会管用:
settingVC.hidesBottomBarWhenPushed = YES ;
[self.navigationController pushViewController:settingVC animated:YES] ;
}
#pragma mark - <UICollectionViewDataSource>
//刷新表格[self.collectionView reloadData]之后就会调用数据源协议代理方法方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.dataSourceArray.count ; //一共35个item ;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
MeSquareCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath] ;
cell.backgroundColor = [UIColor whiteColor] ;
//把这些模型的数据展示到对应的cell上:
cell.meSquareModel = self.dataSourceArray[indexPath.item] ;
return cell ;
}
#pragma mark - <UICollectionViewDelegate>
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"被调用方法为: %s , 所在行数为:第 %zd 行" , __FUNCTION__ , __LINE__) ;
//跳转界面:push到webView:
/*
1.safari: openURL方法 ;
2.UIWebView: 展示网页 ;
3.safair框架--#import <SafariServices/SafariServices.h> //iOS9新出的safair框架
SFSafariViewController:专门用来展示网页的控制器 ;
*/
MeSquareModel *squareModel = self.dataSourceArray[indexPath.item] ;
NSURL *url = [NSURL URLWithString:squareModel.url] ;
//有些事跳转到网页的 , 有些是跳转到其他界面的 , 只需要判断url字符串里有没有http字段:
if (![squareModel.url containsString:@"http"]) return ;
SFSafariViewController *safariVC = [[SFSafariViewController alloc] initWithURL:url] ;
// safariVC.delegate = self ;
[self presentViewController:safariVC animated:YES completion:nil] ;
}
//#pragma mark - <SFSafariViewControllerDelegate>
//推荐我们用modal方式(就不用遵守这个代理协议了!)
//- (void)safariViewControllerDidFinish:(SFSafariViewController *)controller {
//// [self.navigationController popViewControllerAnimated:YES] ;
// [self dismissViewControllerAnimated:YES completion:nil] ;
//}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
愿编程让这个世界更美好
网友评论