iOS开发之高级视图—— UICollectionViewController
2016-05-27 09:20
405 查看
可以继承UICollectionViewController来简化使用UICollectionView。UICollectionViewController中定义了一个 UICollectionView *collectionView,并且实现了 <UICollectionViewDelegate, UICollectionViewDataSource>协议,可以提示开发效率和简化开发。
AppDelegate.m
//
// AppDelegate.m
// UICollectionViewControllerApp
//
// Created by Apple on 16/5/26.
// Copyright © 2016年 Apple. All rights reserved.
//
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
// 创建布局对象,对UICollectionView进行控制
UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc] init];
// 创建ViewController是设置flowLayout
// 如果直接创建不提供布局将包异常 UICollectionView must be initialized with a non-nil layout parameter
ViewController* viewController = [[ViewController alloc] initWithCollectionViewLayout:flowLayout];
//并设置ViewController为rootViewController
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
return YES;
}
@end
ViewController.h
ViewController.m
//
// ViewController.m
// UICollectionViewControllerApp
//
// Created by Apple on 16/5/26.
// Copyright © 2016年 Apple. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
static NSString * const reuseIdentifier = @"Cell";
NSMutableArray* imgNames;
- (void)viewDidLoad {
[super viewDidLoad];
imgNames = [[NSMutableArray alloc] init];
for (int i=1;i<=8; i++) {
[imgNames addObject:[NSString stringWithFormat:@"%d.jpg",i]];
}
[self.collectionView setBackgroundColor:[UIColor whiteColor]];
// Register cell classes
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
}
#pragma mark <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [imgNames count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
// 创建一个UIImageView
UIImageView* imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imgNames [indexPath.row]]];
// 设置cell的背景
cell.backgroundView = imgView;
return cell;
}
#pragma mark - UICollectionViewDelegateFlowLayout
//返回每个cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return CGSizeMake(self.view.frame.size.width/2-20, self.view.frame.size.height/3-10);
}
//设置每一个Cell的垂直和水平间距
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
//top
//left
//bottom
//right
return UIEdgeInsetsMake(10, 5, 10, 5);
}
#pragma mark <UICollectionViewDelegate>
/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
*/
/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
*/
/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
return NO;
}
- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
}
*/
@end
效果图如下:
可以看到实现的效果与上一篇博客的效果一样的。
AppDelegate.m
//
// AppDelegate.m
// UICollectionViewControllerApp
//
// Created by Apple on 16/5/26.
// Copyright © 2016年 Apple. All rights reserved.
//
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
// 创建布局对象,对UICollectionView进行控制
UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc] init];
// 创建ViewController是设置flowLayout
// 如果直接创建不提供布局将包异常 UICollectionView must be initialized with a non-nil layout parameter
ViewController* viewController = [[ViewController alloc] initWithCollectionViewLayout:flowLayout];
//并设置ViewController为rootViewController
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
return YES;
}
@end
ViewController.h
// // ViewController.h // UICollectionViewControllerApp // // Created by Apple on 16/5/26. // Copyright © 2016年 Apple. All rights reserved. // #import <UIKit/UIKit.h> /* 简化使用UICollectionView,可以继承UICollectionViewController UICollectionViewController中定义了一个 UICollectionView *collectionView 并且实现了 <UICollectionViewDelegate, UICollectionViewDataSource>协议 */ @interface ViewController : UICollectionViewController<UICollectionViewDelegateFlowLayout> @end
ViewController.m
//
// ViewController.m
// UICollectionViewControllerApp
//
// Created by Apple on 16/5/26.
// Copyright © 2016年 Apple. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
static NSString * const reuseIdentifier = @"Cell";
NSMutableArray* imgNames;
- (void)viewDidLoad {
[super viewDidLoad];
imgNames = [[NSMutableArray alloc] init];
for (int i=1;i<=8; i++) {
[imgNames addObject:[NSString stringWithFormat:@"%d.jpg",i]];
}
[self.collectionView setBackgroundColor:[UIColor whiteColor]];
// Register cell classes
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
}
#pragma mark <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [imgNames count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
// 创建一个UIImageView
UIImageView* imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imgNames [indexPath.row]]];
// 设置cell的背景
cell.backgroundView = imgView;
return cell;
}
#pragma mark - UICollectionViewDelegateFlowLayout
//返回每个cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return CGSizeMake(self.view.frame.size.width/2-20, self.view.frame.size.height/3-10);
}
//设置每一个Cell的垂直和水平间距
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
//top
//left
//bottom
//right
return UIEdgeInsetsMake(10, 5, 10, 5);
}
#pragma mark <UICollectionViewDelegate>
/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
*/
/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
*/
/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
return NO;
}
- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
}
*/
@end
效果图如下:
可以看到实现的效果与上一篇博客的效果一样的。
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 讲解iOS开发中基本的定位功能实现
- iOS中定位当前位置坐标及转换为火星坐标的方法
- js判断客户端是iOS还是Android等移动终端的方法
- iOS应用开发中AFNetworking库的常用HTTP操作方法小结
- iOS应用中UISearchDisplayController搜索效果的用法
- IOS开发环境windows化攻略
- iOS应用中UITableView左滑自定义选项及批量删除的实现
- iOS中UIAlertView警告框组件的使用教程
- 浅析iOS应用开发中线程间的通信与线程安全问题
- 检测iOS设备是否越狱的方法
- .net平台推送ios消息的实现方法
- 探讨Android与iOS,我们将何去何从?
- Android、iOS和Windows Phone中的推送技术详解