iOS开发之高级视图—— UICollectionViewController
来源:互联网 发布:淘宝骑行头盔哪家靠谱 编辑:程序博客网 时间:2024/05/16 07:39
可以继承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.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// UICollectionViewControllerApp//// Created by Apple on 16/5/26.// Copyright © 2016年 Apple. All rights reserved.//#import "ViewController.h"@interface ViewController ()@end@implementation ViewControllerstatic 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
效果图如下:
可以看到实现的效果与上一篇博客的效果一样的。
1 0
- iOS开发之高级视图—— UICollectionViewController
- iOS开发之高级视图—— UITableView常用方法
- iOS开发之高级视图——UISearchBar
- iOS开发之高级视图—— UISearchController
- iOS开发之高级视图——UINavigationBar
- iOS开发之高级视图—— UITabBarController
- iOS开发之高级视图—— UICollectionView
- iOS开发之高级视图—— UITableView(五)表视图索引
- iOS开发之高级视图—— UITableView(六)表视图刷新
- iOS开发之高级视图—— UITableView操作——新增
- iOS开发之高级视图—— UITableView操作——删除
- iOS开发之高级视图—— UITableView操作——移动
- iOS开发之高级视图—— UITableView的简化操作——UITableViewController
- iOS开发之高级视图—— UITableView(一)简单例子
- iOS开发之高级视图—— UITableView(二)编辑选中行
- iOS开发之高级视图—— UITableView(三)读取plist文件
- iOS开发之高级视图—— UITableView(四)自定义Cell
- iOS开发之高级视图—— UINavigationController(一)简单导航功能实现
- FastDFS + Nginx 多服务器完整配置与反向代理缓存等
- Mac下真机调试
- DUFF'S DEVICE 达夫设备循环写法
- Dubbo 管理员手册
- 【Web动画】CSS3 3D 行星运转 && 浏览器渲染原理
- iOS开发之高级视图—— UICollectionViewController
- 有关webview与javascript交互的方法
- fedora/ubuntu使用checkinstall源码安装软件
- java读取Properties属性文件
- No package identifier when getting value for resource num 0x00000000 【安卓资源找不到】
- android打包apk时报错"已安装了存在签名冲突的同名数据包"
- 如何在程序中判断VS的版本?
- 【1】深度学习之饮水思源:从感知机到深度网络
- Android之内存优化(一)