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

相关文章
相关标签/搜索