iOS使用UIScrollView實現左右滑動UITableView和UICollectionView

在UIScrollView嵌套UITableView這篇文章是很是,但該項目的需求,須要嵌套UICollectionView,和UICollectionView和UITableView有很是多的不一樣,有些知識到如今也沒搞清楚,一遍又一遍的嘗試,最後作出來的。ios

圖:工具


由於本人剛剛接觸ios開發,很是多原理還說不清,因此如下的步驟以圖片爲主。文章結尾會附上源代碼地址。可下載自行研究!
atom

一、新建項目
spa


二、改動storyboard,由於要使用到導航欄,因此刪除原有view,從工具箱中拖入NavigationController。並將入口(剪頭)指向該view。刪除自帶的tableviewcontroller,拖入view controller。例如如下圖
.net


三、新建tableviewcontroller,tableviewcontroller默認帶有tableview的視圖,因此不需要勾選「also create xib file」;但是collection viewcontroller就不行。這點比較鬱悶。
code


四、UICollectionViewController不能直接使用,測試了很是久。就是不能嵌套在scrollview中。因此僅僅能先建立view controller,再包括collection view,需要建立xib文件;打開xib文件拖入Collection View,並將此視圖關聯至
blog

@property (weaknonatomicIBOutletUICollectionView *collection;
圖片


五、collectionviewcontroller就比較麻煩了。首先建立CollectionView所使用的單元格CollectionViewCell;並新建一個空的xib;
ip



六、打開CollectionCell.xib,從工具箱拖入Collection Cell。設置背景色爲黃色,並拖入一個label控件;注意設置Collection Cell的class 爲剛纔創建的「CollectionCell」類(不是Files Owner);關聯開發

IBOutletUILabel *label

。例如如下圖所看到的


至此。所有頁面及前臺已經設置完成

八、先搞定tableviewcontroller,例如如下代碼

//
//  TMJTableViewController.m
//  PageTest
//
//  Created by ejiang on 14-6-30.
//  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

九、仍是看源代碼吧,粘貼代碼沒意思,主要步驟就以上幾部

源代碼下載地址:http://download.csdn.net/detail/wuwo333/8098431

相關文章
相關標籤/搜索