iOS CollectionView实现屏幕左右滑动
2016-07-27 00:00
423 查看
摘要: 将UITableView嵌入到UICollectionViewCell中,一个CollectionViewCell即为一个屏幕,在Cell中嵌入TableView用来显示将要展示的界面或数据
由于代码量较大,本文主要简单讲解主要步骤,如有疑问可以留言或私信.
一,创建自定义UICollectionViewCell,并实现TableView的数据源,代理方法.
二, 在CollectionViewCell中实现TableView的数据源和代理方法
三, 创建UIViewController控制器并继承Collection协议与代理方法,创建UICollectionView,并注册自定义CollectionViewCell
四, 实现数据源,代理方法:
由于代码量较大,本文主要简单讲解主要步骤,如有疑问可以留言或私信.
一,创建自定义UICollectionViewCell,并实现TableView的数据源,代理方法.
#import <UIKit/UIKit.h> @interface AKCollectionCellFirst : UICollectionViewCell<UITableViewDelegate,UITableViewDataSource> @property (nonatomic,strong) UITableView *tabView; @end #import "AKCollectionCellFirst.h" @interface AKCollectionCellFirst() @end @implementation AKCollectionCellFirst - (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self){ _tabView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)]; _tabView.separatorStyle = UITableViewCellSeparatorStyleNone; _tabView.delegate = self; _tabView.dataSource = self; //如果需要自定义tableViewCell可以在此对其进行注册 [_tabView registerClass:[AKTableCellFirst class] forCellReuseIdentifier:@"ONE"]; [self addSubview:_tabView]; } return self; }
二, 在CollectionViewCell中实现TableView的数据源和代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 10; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 120; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ AKTableCellFirst *cell = [tableView dequeueReusableCellWithIdentifier:@"ONE"]; if (cell == nil){ cell = [[AKTableCellFirst alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ONE"]; } NSArray *arrayP = @[@"Kuture",@"CD",@"ER",@"TE",@"WEB",@"AUI",@"HG",@"TYT",@"FHB",@"AW"]; cell.person.text = arrayP[indexPath.row]; cell.time.text = @"2016-07-11 15:08"; cell.flowChat.text = @"What?"; cell.content.text = @"You Know Who I am And you Find SomeThing,What?You find again ? I do not Belive that"; cell.content.numberOfLines = 0; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; return cell; }
三, 创建UIViewController控制器并继承Collection协议与代理方法,创建UICollectionView,并注册自定义CollectionViewCell
#import "AKCollection.h" @interface AKCollection()<UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource> @end @implementation AKCollection - (void)viewDidLoad{ [super viewDidLoad]; UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init]; flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal; flowLayout.itemSize = CGSizeMake(SCREEN_W, SCREEN_H - 150); flowLayout.minimumLineSpacing = 0; UICollectionView *collec = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:flowLayout]; _collec = collec; _collec.delegate = self; _collec.dataSource = self; _collec.bounces = NO; _collec.backgroundColor = GROUND_COLOR(242, 242, 242, 1); _collec.alpha = 0.95; _collec.pagingEnabled = YES; //注册自定义的UICollectionViewCell [_collec registerClass:[AKCollectionCellFirst class] forCellWithReuseIdentifier:@"CellFirst"]; [_collec registerClass:[AKCollectCellSecond class] forCellWithReuseIdentifier:@"CellSecond"]; [_collec registerClass:[A 3ff0 KCollectCellThird class] forCellWithReuseIdentifier:@"CellThird"]; [_collec registerClass:[AKCollectCellFourth class] forCellWithReuseIdentifier:@"CellFourth"]; [self.view addSubview:_collec]; }
四, 实现数据源,代理方法:
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ return 1; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return 4; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ UICollectionViewCell *cell; NSArray *idArray = @[@"CellFirst",@"CellSecond",@"CellThird",@"CellFourth"]; cell = [collectionView dequeueReusableCellWithReuseIdentifier:idArray[indexPath.item] forIndexPath:indexPath]; return cell; }
相关文章推荐
- 峰回路转,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 App开发中的UISegmentedControl分段组件用法总结
- IOS开发环境windows化攻略
- iOS应用中UITableView左滑自定义选项及批量删除的实现
- iOS中UIAlertView警告框组件的使用教程
- 浅析iOS应用开发中线程间的通信与线程安全问题
- iOS中的UIKeyboard键盘视图使用方法小结
- 检测iOS设备是否越狱的方法
- .net平台推送ios消息的实现方法