iOS使用UIScrollView實現左右滑動UITableView和UICollectionView
來源:程序員人生 發布時間:2014-11-07 08:42:55 閱讀次數:4778次
在UIScrollView中嵌套UITableView的文章很多,但是項目需要,需要嵌套UICollectionView,而UICollectionView和UITableView有很多不同,有些知識到現在也沒弄清楚,1遍1遍的嘗試,總算是做出來了。以下是實現后的效果圖:


由于本人剛剛接觸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 (weak, nonatomic) IBOutletUICollectionView *collection;

5、collectionviewcontroller就比較麻煩了,首先創建CollectionView所使用的單元格CollectionViewCell;并新建1個空的xib;


6、打開CollectionCell.xib,從工具箱拖入Collection Cell,設置背風景為黃色,并拖入1個label控件;注意設置Collection Cell的class 為剛才建立的“CollectionCell”類(不是Files Owner);關聯
IBOutletUILabel *label
;以下圖所示

至此,所有頁面及前臺已設置終了
8、先弄定tableviewcontroller,以下代碼
//
// TMJTableViewController.m
// PageTest
//
// Created by ejiang on 14⑹⑶0.
// Copyright (c) 2014年 daijier. All rights reserved.
//
#import "TMJTableViewController.h"
@interfaceTMJTableViewController ()
@end
@implementation TMJTableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
returnself;
}
- (void)viewDidLoad
{
[superviewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier=@"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell==nil)
{
cell=[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellIdentifier];
}
cell.textLabel.text=@"哈哈";
return cell;
}
@end
9、還是看源碼吧,粘貼代碼沒意思,主要步驟就以上幾部
源碼下載地址:http://download.csdn.net/detail/wuwo333/8098431
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈