标签:
效果图如下:
要想实现上述效果,需要先有一个继承于NSObject的Model类(这里起名叫DestinationModel)用来赋值,需要一个继承于UICollectionViewCell的类(DestinCollectionViewCell)用来自定义cell,还需要一个继承于UIView的类(DestinationView)用来显示数据,还需要一个继承于UIViewController的类(DestinationViewController),最后需要一个继承于UICollectionReusableView的类(DestinCollectionReusableView)用于显示标题
Model类中的写法
DestinationModel.h
#import <Foundation/Foundation.h>
@interface DestinationModel : NSObject
///接收图片
@property(nonatomic,strong)NSString *cover;
///接收城市名
@property(nonatomic,strong)NSString *name;
@end
DestinationModel.m
#import "DestinationModel.h"
@implementation DestinationModel
//防止崩溃
- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{
}
@end
DestinationView中的写法
DestinationView.h
#import <UIKit/UIKit.h>
@interface DestinationView : UIView
@property (nonatomic,strong)UICollectionView *destinView;
@end
DestinationView.m
#import "DestinationView.h"
@implementation DestinationView
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
[self addAllViews];
}
return self;
}
- (void)addAllViews{
UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc]init];
// 每个item 的大小
flowLayout.itemSize = CGSizeMake(self.frame.size.width / 2 - 17 ,self.frame.size.height *0.25);
//每个item的距离
flowLayout.sectionInset=UIEdgeInsetsMake(10, 10, 10, 10);
//竖屏滑动
flowLayout.scrollDirection=UICollectionViewScrollDirectionVertical;
self.destinView=[[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:flowLayout];
self.destinView.backgroundColor = [UIColor whiteColor];
[self addSubview:self.destinView];
}
@end
DestinCollectionViewCell
DestinCollectionViewCell.h
#import <UIKit/UIKit.h>
@interface DestinCollectionViewCell : UICollectionViewCell
///图片
@property (nonatomic,strong) UIImageView *imageView;
///item所在的indexpath
@property (nonatomic,strong) UILabel *itemLabel;
@end
DestinCollectionViewCell.m
#import "DestinCollectionViewCell.h"
@implementation DestinCollectionViewCell
-(instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
[self addSubview:self.imageView];
self.itemLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, self.frame.size.height-45, self.frame.size.width,self.frame.size.height*0.28)];
self.itemLabel.textAlignment = NSTextAlignmentCenter;
self.itemLabel.textColor = [UIColor whiteColor];
[self.imageView addSubview:self.itemLabel];
}
return self;
}
@end
DestinationViewController
DestinationViewController.h
#import <UIKit/UIKit.h>
@class DestinCollectionViewCell;
#import "DestinationView.h"
@interface DestinationViewController : UIViewController
@property(nonatomic,strong)DestinCollectionViewCell *collection;
@property(nonatomic,strong)DestinationView *desrinV;
@end
DestinationViewController.m
//
// DestinationViewController.m
// travel1.0
//
// Created by lanou3g on 16/3/25.
// Copyright © 2016年 浮夸. All rights reserved.
//
#import "DestinationViewController.h"
#import "DestinCollectionViewCell.h"
#import "DestinationModel.h"
#import "DestinCollectionReusableView.h"
@interface DestinationViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
///接收title
@property (nonatomic,strong)NSMutableArray *keyArray;
///接收所有数据
@property (nonatomic,strong)NSMutableDictionary *allDict;
@end
@implementation DestinationViewController
//懒加载
- (NSMutableArray *)keyArray
{
if (_keyArray ==nil) {
_keyArray = [NSMutableArray new];
}
return _keyArray;
}
//懒加载
- (NSMutableDictionary *)allDict
{
if (_allDict == nil) {
_allDict = [NSMutableDictionary new];
}
return _allDict;
}
- (void)loadView{
self.desrinV = [[DestinationView alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.view = self.desrinV;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self setDelegate];
[self setData];
[self setViews];
}
- (void)setDelegate
{
self.desrinV.destinView.delegate = self;
self.desrinV.destinView.dataSource = self;
}
- (void)setData
{
NSURL *url = [NSURL URLWithString:@"http://api.breadtrip.com/destination/v3/"];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:urlRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSArray *mainArray = dict[@"elements"];
for (NSDictionary *dict1 in mainArray) {
NSString *key = dict1[@"title"];//
[self.keyArray addObject:key]; //
NSArray *secArray = dict1[@"data"];
NSMutableArray *tempArray = [NSMutableArray new]; //
for (NSDictionary *dict2 in secArray) {
DestinationModel *model = [[DestinationModel alloc] init];
[model setValuesForKeysWithDictionary:dict2];
[tempArray addObject: model];
}
[self.allDict setObject:tempArray forKey:key];//
}
//回到主线程
[self performSelectorOnMainThread:@selector(mian) withObject:self waitUntilDone:YES];
}];
[task resume];
}
- (void)mian{
//刷新列表
[self.desrinV.destinView reloadData];
}
- (void)setViews
{
//注册cell
[self.desrinV.destinView registerClass:[DestinCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
//注册header
[self.desrinV.destinView registerClass:[DestinCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"cell_id"];
}
//实现代理方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return self.keyArray.count;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
NSString *key = self.keyArray[section];
return [self.allDict[key] count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
DestinCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
NSString *key = self.keyArray[indexPath.section];
DestinationModel *model = self.allDict[key][indexPath.row];
cell.itemLabel.text = model.name;
cell.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:model.cover]]];
return cell;
}
//实现dataSource代理方法
//collection大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
return CGSizeMake(self.desrinV.destinView.bounds.size.width, 30);
}
//header
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
DestinCollectionReusableView *headerView =[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"cell_id" forIndexPath:indexPath];
headerView.titleLabel.text = self.keyArray[indexPath.section];
return headerView;
}
@end
标签:
原文地址:http://www.cnblogs.com/lidongtian/p/5322024.html