UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"XFJMeViewController" bundle:nil];
//载入箭头指向的控制器
XFJMeViewController *meVC = [storyBoard instantiateInitialViewController];
XFJNavigationController *nav4 = [[XFJNavigationController alloc] initWithRootViewController:meVC];
[self addChildViewController:nav4];
#pragma mark - 加入尾部视图
- (void)setUpFooterView
{
//流水布局
UICollectionViewFlowLayout *flowLayout = ({
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
//设置尺寸
flowLayout.itemSize = CGSizeMake(XFJ_itemsWH ,XFJ_itemsWH);
//设置垂直间距和水平间距
flowLayout.minimumInteritemSpacing = margin;
flowLayout.minimumLineSpacing = margin;
flowLayout;
});
//创建collectionView
UICollectionView *collectionView = ({
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:flowLayout];
//设置背景颜色
collectionView.backgroundColor = XFJ_globeColor;
//设置尾部视图为collectionView
self.tableView.tableFooterView = collectionView;
//设置数据源代理
collectionView.dataSource = self;
//设置代理
collectionView.delegate = self;
//赋值
self.collectionView = collectionView;
//设置collectionView不能滚动
collectionView.scrollEnabled = NO;
collectionView;
});
//载入xib的时候注冊用
[collectionView registerNib:[UINib nibWithNibName:@"XFJMeCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:ID];
}
//icon(头像);name(姓名);url(网址)
@property (nonatomic, strong) NSString *icon;
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *url;
#pragma mark - 载入数据
- (void)setUpData
{
//查看接口文档:请求方式:GET 请求地址:http://api.budejie.com/api/api_open.php
//请求參数:a = square ; c = topic
//模型參数:icon;name;url
//创建会话管理者
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//设置请求參数
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
//包装请求參数
dict[@"a"] = @"square";
dict[@"c"] = @"topic";
//发送请求
[manager GET:@"http://api.budejie.com/api/api_open.php" parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
//因为是字典数组.那么取出数组来转
NSArray *array = responseObject[@"square_list"];
//运用mj框架字典转模型
self.meItems = [XFJMeItem mj_objectArrayWithKeyValuesArray:array];
//写入plist文件,方便阅读
[responseObject writeToFile:@"/Users/xiaofeng/Desktop/BuDeJie/Me.plist" atomically:YES];
//调用处理数据的方法
[self sloveData];
//刷新表格
[self.collectionView reloadData];
//计算collectionView的高度
//取出模型数组中的元素
NSInteger count = self.meItems.count;
//计算行数
NSInteger rows = (count - margin) / cols + margin;
//计算collectionView的总高度
CGFloat cellH = XFJ_itemsWH * rows + (rows - margin) * margin;
//collectionView的总高度
self.collectionView.XFJ_Height = cellH;
//依据内容自适应
self.tableView.tableFooterView = self.collectionView;
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"error");
}];
}
#pragma mark - collectionView数据源方法(组数)--->能够不写,默觉得1
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
#pragma mark - 行数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.meItems.count;
}
#pragma mark - cell的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
XFJMeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
cell.meItem = self.meItems[indexPath.row];
return cell;
}
#pragma mark - 提供模型的set方法
- (void)setMeItem:(XFJMeItem *)meItem
{
_meItem = meItem;
[self.iconImageView sd_setImageWithURL:[NSURL URLWithString:meItem.icon]];
self.nameLabel.text = meItem.name;
}
static NSString * const ID = @"cell";
static NSInteger const cols = 4;
static CGFloat const margin = 1;
#define XFJ_itemsWH (XFJ_screenW - (cols - 1) * margin) / cols
//设置代理
collectionView.delegate = self;
#pragma mark - 代理方法(点击某个cell调用)
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"点击了cell");
//取出相应的cell
XFJMeItem *meItem = self.meItems[indexPath.row];
//推断字符串中是否含有http(假设不含有了就直接return)
if (![meItem.url containsString:@"http"]) {
return;
}
//含有了在运行
NSURL *url = [NSURL URLWithString:meItem.url];
//创建safari控制器(第一种方式)
SFSafariViewController *safari = [[SFSafariViewController alloc] initWithURL:url];
//mdel出控制器
// [self presentViewController:safari animated:YES completion:nil];
[self.navigationController pushViewController:safari animated:YES];
self.navigationController.navigationBarHidden = YES;
//设置代理
safari.delegate = self;
}
#pragma mark - safari的代理方法
- (void)safariViewControllerDidFinish:(SFSafariViewController *)controller
{
[self.navigationController popViewControllerAnimated:YES];
// [self dismissViewControllerAnimated:YES completion:nil];
self.navigationController.navigationBarHidden = NO;
}
//另外一种方法:(ios8才有)
//创建控制器
XFJWebViewController *webViewController = [[XFJWebViewController alloc] init];
//赋值
webViewController.url = url;
//push
[self.navigationController pushViewController:webViewController animated:YES];
- (void)viewDidLoad {
[super viewDidLoad];
//创建UIWebView对象
WKWebView *webView = [[WKWebView alloc] init];
//赋值
self.webView = webView;
//设置尺寸
webView.frame = self.view.bounds;
//将webView插入到view中
[self.view insertSubview:webView atIndex:0];
//创建请求
NSURLRequest *request = [NSURLRequest requestWithURL:self.url];
//请求
[webView loadRequest:request];
//监听web中的进度(KVO)----self去监听webView的estimatedProgress值的变化
[webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
}
//仅仅要监听值的变化,就会调用
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
//进度条的变化
self.progressView.progress = self.webView.estimatedProgress;
//控制进度条
self.progressView.hidden = self.progressView.progress >= 1;
}
#pragma mark - 移除监听
- (void)dealloc
{
[self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
}
十三 细节处理
//分组样式默认头部和尾部都有一定的滚动区域
self.tableView.sectionFooterHeight = 10;
self.tableView.sectionHeaderHeight = 0;
//设置顶部额外滚动区域
self.tableView.contentInset = UIEdgeInsetsMake(-25, 0, 0, 0);
#pragma mark - 处理数据(往最后的空格中加入空的模型)
- (void)sloveData
{
//取出模型中的数据
NSInteger count = self.meItems.count;
//计算空格数
NSInteger exte = count % cols;//9 % 4 = 1;
//补充模型
if (exte) {
exte = cols - exte;
//创建空的模型
for (int i = 0; i < exte; i++) {
XFJMeItem *meItem = [[XFJMeItem alloc] init];
//加入模型
[self.meItems addObject:meItem];
}
}
}
//
// XFJFileManager.h
// BuDeJie
//
// Created by 肖锋 on 12/4/6.
// Copyright © 2012年 XFJ. All rights reserved.
//
/**
* 专门用于处理文件业务
*
* 使用方法:1 先获取目录尺寸
*
* 2 删除目录全部的文件
*/
#import <Foundation/Foundation.h>
@interface XFJFileManager : NSObject
/**
* 获取目录尺寸
*
* @param directoryPath 目录全路径
*
* @return 目录尺寸
*/
+ (NSInteger)getDirectorySize:(NSString *)directoryPath;
/**
* 删除目录下全部文件
*
* @param directoryPath 目录全路径
*/
+ (void)removeDirectoryPath:(NSString *)directoryPath;
@end
//
// XFJFileManager.m
// BuDeJie
//
// Created by on 12/4/6.
// Copyright © 2012年 肖锋. All rights reserved.
//
#import "XFJFileManager.h"
@implementation XFJFileManager
+ (void)removeDirectoryPath:(NSString *)directoryPath
{
NSFileManager *mgr = [NSFileManager defaultManager];
BOOL isDirectory;
BOOL isExist = [mgr fileExistsAtPath:directoryPath isDirectory:&isDirectory];
if (!isExist || !isDirectory) {
// 报错:抛异常
NSException *excp = [NSException exceptionWithName:@"filePathError" reason:@"传错,必须传目录路径" userInfo:nil];
[excp raise];
}
NSArray *subpaths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryPath error:nil];
for (NSString *subPath in subpaths) {
NSString *filePath = [directoryPath stringByAppendingPathComponent:subPath];
[[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
}
}
// 获取目录尺寸
+ (NSInteger)getDirectorySize:(NSString *)directoryPath
{
// 获取文件管理者
NSFileManager *mgr = [NSFileManager defaultManager];
BOOL isDirectory;
BOOL isExist = [mgr fileExistsAtPath:directoryPath isDirectory:&isDirectory];
if (!isExist || !isDirectory) {
// 报错:抛异常
NSException *excp = [NSException exceptionWithName:@"filePathError" reason:@"传入文件路径错误.." userInfo:nil];
[excp raise];
}
/*
获取这个目录中全部文件路径,然后累加 = 目录的尺寸
*/
// 获取目录下全部的文件
NSArray *subpaths = [mgr subpathsAtPath:directoryPath];
NSInteger totalSize = 0;
for (NSString *subpath in subpaths) {
// 拼接文件全路径
NSString *filePath = [directoryPath stringByAppendingPathComponent:subpath];
// 排除目录
BOOL isDirectory;
BOOL isExist = [mgr fileExistsAtPath:filePath isDirectory:&isDirectory];
if (!isExist || isDirectory) continue;
// 隐藏文件
if ([filePath containsString:@".DS"]) continue;
// 指定路径获取这个路径的属性
// attributesOfItemAtPath:仅仅能获取文件属性
NSDictionary *attr = [mgr attributesOfItemAtPath:filePath error:nil];
NSInteger size = [attr fileSize];
totalSize += size;
}
return totalSize;
}
@end
手机扫一扫
移动阅读更方便
你可能感兴趣的文章