美文网首页
2021-03-01

2021-03-01

作者: 金天动地 | 来源:发表于2021-03-01 23:56 被阅读0次

'''
//
// GridViewController.m
// VTMagicView
//
// Created by tianzhuo on 14/12/30.
// Copyright (c) 2014年 tianzhuo. All rights reserved.
//

import "VTGridViewController.h"

import "VTDetailViewController.h"

import <VTMagic/VTMagic.h>

import "VTGridViewCell.h"

import "DataManager.h"

define IPHONELESS6 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? 640 == [[UIScreen mainScreen] currentMode].size.width : NO)

static NSString *reuseIdentifier = @"grid.reuse.identifier";

@interface VTGridViewController()

@property (nonatomic, strong) NSMutableArray *infoList;

@end

@implementation VTGridViewController

  • (instancetype)init {
    // 1. 是iPhone设备 mark byKing
    BOOL iPhoneDevice = kiPhoneDevice;
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.sectionInset = iPhoneDevice ?UIEdgeInsetsMake(10, 10, 10, 10) : UIEdgeInsetsMake(20, 20, 20, 20);
    layout.itemSize = iPhoneDevice ? (IPHONELESS6 ? CGSizeMake(140, 175) : CGSizeMake(115, 170)) : CGSizeMake(188, 188);
    layout.minimumInteritemSpacing = iPhoneDevice ? 2 : 11;
    layout.minimumLineSpacing = iPhoneDevice ? 10 : 11;
    // collecitonVC默认初始方法 mark byKing
    return [super initWithCollectionViewLayout:layout];
    }

  • (void)viewDidLoad {
    [super viewDidLoad];

    // ??? 为啥scrollsToTop = NO 还是会滚动. 视图显示的时候重新设置为YES
    self.collectionView.scrollsToTop = NO;
    self.collectionView.backgroundColor = RGBCOLOR(239, 239, 239);
    self.collectionView.contentInset = UIEdgeInsetsMake(0, 0, VTTABBAR_HEIGHT, 0);
    [self.collectionView registerClass:[VTGridViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    }

// 复用情况步骤分析: >>> 1.viewWillAppear

  • (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    VTPRINT_METHOD
    }
    // 复用情况步骤分析: >>> 2.viewDidAppear

  • (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    [self refreshPageIfNeeded];
    self.collectionView.scrollsToTop = YES;
    NSInteger pageIndex = [self vtm_pageIndex];
    NSLog(@"current page index is: %ld", (long)pageIndex);
    VTPRINT_METHOD
    }

  • (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    [self cancelNetworkRequest];
    self.collectionView.scrollsToTop = NO;
    VTPRINT_METHOD
    }

  • (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];

    [self savePageInfo];
    VTPRINT_METHOD
    }

pragma mark UICollectionViewDataSource

  • (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _infoList.count;
    }

  • (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    VTGridViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    NSString *imageName = _infoList[indexPath.item];
    [cell.imageView setImage:[UIImage imageNamed:imageName]];
    cell.commentLabel.text = [NSString stringWithFormat:@"%d人出游", arc4random_uniform(9999)];
    cell.titleLabel.text = @"景点介绍,景点介绍,景点介绍。。";
    return cell;
    }

  • (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger currentPage = self.magicController.currentPage;
    NSLog(@"==didSelectItemAtIndexPath%@ \n current page is: %ld==", indexPath, (long)currentPage);
    VTDetailViewController *detailViewController = [[VTDetailViewController alloc] init];
    detailViewController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detailViewController animated:YES];
    }

pragma mark - VTMagicReuseProtocol

  • (void)vtm_prepareForReuse {
    // reset content offset
    NSLog(@"clear old data if needed:%@", self);
    [_infoList removeAllObjects];
    [self.collectionView reloadData];
    [self.collectionView setContentOffset:CGPointZero];
    }

pragma mark - functional methods

  • (void)refreshPageIfNeeded { // 一小时进行刷新数据.
    NSTimeInterval currentStamp = [[NSDate date] timeIntervalSince1970];
    // 一小时内不做刷新 mark byKing
    if (self.infoList.count && currentStamp - _menuInfo.lastTime < 5) { //60 * 60
    return;
    }

    /*

    1. 第一次 _menuInfo.lastTime = 0, currentStamp 很大 肯定能刷新数据
    2. 第二次 _menuInfo.lastTime = currentStamp 必须等一个小时才能刷新数据

    */

    // 模拟网络请求延时,根据_menuInfo.menuId请求对应菜单项的相关信息
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.02 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    _menuInfo.lastTime = currentStamp;
    [self handleNetworkSuccess];
    });
    }

  • (void)cancelNetworkRequest {
    // 由于页面可能会被重用,需要取消不必要的网络请求
    // NSLog(@"maybe you should cancel network request in here");
    }

  • (void)handleNetworkSuccess {
    NSLog(@"==模拟网络请求成功后刷新页面==");
    for (NSInteger index = 0; index < 50; index++) {
    [self.infoList addObject:[NSString stringWithFormat:@"image_%d", arc4random_uniform(13)]];
    }
    [self.collectionView reloadData];

}

  • (void)savePageInfo {
    [[DataManager sharedInstance] savePageInfo:_infoList menuId:_menuInfo.menuId];
    }

// MARK: 设置menuInfo的时候加载本地数据

  • (void)loadLocalData {
    NSArray *cacheList = [[DataManager sharedInstance] pageInfoWithMenuId:_menuInfo.menuId];
    [_infoList addObjectsFromArray:cacheList];
    [self.collectionView reloadData];
    }

pragma mark - accessor methods

  • (void)setMenuInfo:(MenuInfo *)menuInfo {
    _menuInfo = menuInfo;
    // 复用VC步骤分析: >>> 3. 设置菜单信息的时候加载本地数据.
    [self loadLocalData];
    }

  • (NSMutableArray *)infoList {
    if (!_infoList) {
    _infoList = [[NSMutableArray alloc] init];
    }
    return _infoList;
    }

@end

'''

相关文章

  • 前端面试每日 3+1 —— 第685天

    今天的知识点 (2021-03-01) —— 第685天 (我也要出题[http://www.h-camel.co...

  • 周一 2021-03-01 23:00 - 06:45 阴雨 1

    周一 2021-03-01 23:00 - 06:45 阴雨 10h25m 记录闪现的灵感(inspiration...

  • 宁愿试错也不要错过

    时间:2021-03-01 打卡人:婷婷 打卡天数:89Day 名言警句:再烂的专科也有人挑灯夜读 再好的本科也有...

  • 春暖花开

    2021-03-01精进第41天 | 没有记录就没有发生 2021年的事业梦想个人目标描述:达成MDRT业绩目标。...

  • 开源节流

    我怎么如此幸运-重生186-戴红霞(2021-03-01) 我怎么如此幸运-开源节流 1.我怎么如此幸运意想不到再...

  • 吾女成长记 2021-3

    流水账似的记录,变成一颗颗珍珠,串成美丽的生活回忆。 ●2021-03-01 从老家回来下高铁坐上回家的地铁,米粒...

  • 03-01

    2021-03-01 Chapter 0 3-1-1准备工作和R语言介绍 1.什么是R 面向用户的一种编程语言,用...

  • 【熊妈·共读琐思】关于“策略”的更多思考

    侯蕾语写,2021-03-01 06:33:15 今天早上习惯和他的策略这个词是由手足象征这部分引发出来的,标题是...

  • 片段

    2021-03-01 【一个人唯一能够拥有的东西】 书籍和电影会使人的寿命延长3倍,时间又会使很多东西显得渺小。所...

  • 2021-03-01 天人合一与心理表征

    【2021-03-01日精进 第285天】表现:9分 精灵老师们讨论天人合一 昨日盘思洞里就天人合一的理解有了不...

网友评论

      本文标题:2021-03-01

      本文链接:https://www.haomeiwen.com/subject/fdozfltx.html