iOS使用UIScrollView实现左右滑动UITableView和UICollectionView

iOS使用UIScrollView实现左右滑动UITableView和UICollectionView

标签: iOS左右滑动UIScrollViewUICollectionView


在UIScrollView中嵌套UITableView的文章很多,但是项目需要,需要嵌套UICollectionView,而UICollectionView和UITableView有很多不同,有些知识到现在也没搞清楚,一遍一遍的尝试,总算是做出来了。以下是实现后的效果图:


由于本人刚刚接触ios开发,很多原理还说不清,所以下面的步骤以图片为主,文章结尾会附上源码地址,可下载自行研究!

1、新建项目


2、修改storyboard,由于要使用到导航栏,所以删除原有view,从工具箱中拖入NavigationController,并将入口(剪头)指向该view;删除自带的tableviewcontroller,拖入view controller;如下图


3、新建tableviewcontroller,tableviewcontroller默认带有tableview的视图,所以不需要勾选“also create xib file”;但是collection viewcontroller就不行,这点比较郁闷!


4、UICollectionViewController不能直接使用,测试了很久,就是不能嵌套在scrollview中,所以只能先创建view controller,再包含collection view,需要创建xib文件;打开xib文件拖入Collection View,并将此视图关联至

@property (weaknonatomicIBOutletUICollectionView *collection;


5、collectionviewcontroller就比较麻烦了,首先创建CollectionView所使用的单元格CollectionViewCell;并新建一个空的xib;




6、打开CollectionCell.xib,从工具箱拖入Collection Cell,设置背景色为黄色,并拖入一个label控件;注意设置Collection Cell的class 为刚才建立的“CollectionCell”类(不是Files Owner);关联

IBOutletUILabel *label

;如下图所示



至此,所有页面及前台已经设置完毕

8、先搞定tableviewcontroller,如下代码


[objc] view plain copy



  1. //  
  2. //  TMJTableViewController.m  
  3. //  PageTest  
  4. //  
  5. //  Created by ejiang on 14-6-30.  
  6. //  Copyright (c) 2014年 daijier. All rights reserved.  
  7. //  
  8.    
  9. #import "TMJTableViewController.h"  
  10.    
  11. @interfaceTMJTableViewController ()  
  12.   
  13.   
  14.    
  15. @end  
  16.    
  17.   
  18. @implementation TMJTableViewController  
  19.   
  20.    
  21.   
  22. - (id)initWithStyle:(UITableViewStyle)style  
  23.   
  24. {  
  25.   
  26. self = [super initWithStyle:style];  
  27.   
  28. if (self) {  
  29.         // Custom initialization  
  30.     }  
  31.     returnself;  
  32.   
  33.   
  34. }  
  35.   
  36.    
  37.   
  38. - (void)viewDidLoad  
  39.   
  40. {  
  41.     [superviewDidLoad];  
  42.   
  43.   
  44. }  
  45.   
  46.    
  47.   
  48. - (void)didReceiveMemoryWarning  
  49.   
  50. {  
  51.     [superdidReceiveMemoryWarning];  
  52.   
  53.   
  54. }  
  55.   
  56.    
  57. #pragma mark - Table view data source  
  58.    
  59.   
  60. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  
  61.   
  62. {  
  63.   
  64. return 1;  
  65.   
  66. }  
  67.   
  68.    
  69.   
  70. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
  71.   
  72. {  
  73.   
  74. return 10;  
  75.   
  76. }  
  77.   
  78.    
  79.   
  80.    
  81.   
  82. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  83.   
  84. {  
  85.   
  86. static NSString *cellIdentifier=@"cell";  
  87.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];  
  88.   
  89.   
  90.     if(cell==nil)  
  91.   
  92. {  
  93.         cell=[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellIdentifier];  
  94.   
  95.   
  96.     }  
  97.     cell.textLabel.text=@"哈哈";  
  98.   
  99.   
  100.     return cell;  
  101.   
  102. }  
  103. @end  



9、还是看源码吧,粘贴代码没意思,主要步骤就以上几部

源码下载地址:http://download.csdn.net/detail/wuwo333/8098431

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值