本文代码例子: http://pan.baidu.com/s/1bnBvnPl
最近项目需要,需要用到左右滑动的TableView,这里做了一个Demo。
原理很简单,就是一个UIScrollView上放两个TableView或多个。放在UIScrollView上。
先创建3个TableView。
OneViewController.h
#import <UIKit/UIKit.h>
@interface OneViewController : UITableViewController
@end
OneViewController.m
//
// OneViewController.m
// 左右滑动的TableView
//
// Created by 杜甲 on 14-1-12.
// Copyright (c) 2014年 杜甲. All rights reserved.
//
#import "OneViewController.h"
@interface OneViewController ()
@end
@implementation OneViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (nil == cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = @"111";
return cell;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
/*
#pragma mark - Navigation
// In a story board-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
TwoViewController.h
#import <UIKit/UIKit.h>
@interface TwoViewController : UITableViewController
@end
TwoViewController.m
//
// TwoViewController.m
// 左右滑动的TableView
//
// Created by 杜甲 on 14-1-12.
// Copyright (c) 2014年 杜甲. All rights reserved.
//
#import "TwoViewController.h"
@interface TwoViewController ()
@end
@implementation TwoViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return 6;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (nil == cell){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = @"99999";
// Configure the cell...
return cell;
}
@end
ThreeViewController.h
#import <UIKit/UIKit.h>
@interface ThreeViewController : UITableViewController
@end
ThreeViewController.m
//
// ThreeViewController.m
// 左右滑动的TableView
//
// Created by 杜甲 on 14-1-12.
// Copyright (c) 2014年 杜甲. All rights reserved.
//
#import "ThreeViewController.h"
@interface ThreeViewController ()
@end
@implementation ThreeViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return 6;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (nil == cell){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = @"888888";
return cell;
}
@end
3个TableView创建完了。
ViewController.h
#import <UIKit/UIKit.h>
#import "OneViewController.h"
#import "TwoViewController.h"
#import "ThreeViewController.h"
@interface ViewController : UIViewController<UIScrollViewDelegate>
@property (strong , nonatomic) UIScrollView* m_ScrollView;
@property (strong , nonatomic) OneViewController* m_OneView;
@property (strong , nonatomic) TwoViewController* m_TwoView;
@property (strong , nonatomic) ThreeViewController* m_ThreeView;
@end
ViewController.m
//
// ViewController.m
// 左右滑动的TableView
//
// Created by 杜甲 on 14-1-12.
// Copyright (c) 2014年 杜甲. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.m_ScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height)];
self.m_ScrollView.contentSize = CGSizeMake(960, 480);
self.m_ScrollView.scrollEnabled = YES;
self.m_ScrollView.delegate = self;
self.m_ScrollView.bounces = YES;
self.m_ScrollView.pagingEnabled = YES;
self.m_ScrollView.showsHorizontalScrollIndicator = NO;
[self.view addSubview:self.m_ScrollView];
self.m_OneView = [[OneViewController alloc] initWithStyle:UITableViewStylePlain];
[self.m_ScrollView addSubview:self.m_OneView.view];
self.m_TwoView = [[TwoViewController alloc] initWithStyle:UITableViewStylePlain];
self.m_TwoView.view.frame = CGRectMake(320, 0, self.view.frame.size.width, self.view.frame.size.height);
[self.m_ScrollView addSubview:self.m_TwoView.view];
self.m_ThreeView = [[ThreeViewController alloc] initWithStyle:UITableViewStylePlain];
self.m_ThreeView.view.frame = CGRectMake(640, 0, self.view.frame.size.width, self.view.frame.size.height);
[self.m_ScrollView addSubview:self.m_ThreeView.view];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end