UITableView that can slide left and right

Source: Internet
Author: User

 

Recently, you need to use TableView with left and right slides. Here is a Demo.

The principle is simple, that is, put two or more tableviews on a UIScrollView. Put it on UIScrollView.

Create three tableviews first.

OneViewController. h

 

#import 
 
  @interface OneViewController : UITableViewController@end
 

OneViewController. m

 

 

//// OneViewController. m // The left and right sliding TableView /// Created by du jia on 14-1-12. // Copyright (c) June 2014 dujia. all rights reserved. // # import OneViewController. h @ interface OneViewController () @ end @ implementation OneViewController-(id) initWithStyle :( UITableViewStyle) style {self = [super initWithStyle: style]; if (self) {// initim 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 progress: 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 
 
  @interface TwoViewController : UITableViewController@end
 

TwoViewController. m

 

 

//// TwoViewController. m // The left and right sliding TableView /// Created by du jia on 14-1-12. // Copyright (c) June 2014 dujia. all rights reserved. // # import TwoViewController. h @ interface TwoViewController () @ end @ implementation TwoViewController-(id) initWithStyle :( UITableViewStyle) style {self = [super initWithStyle: style]; if (self) {// initim 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 progress: cellIdentifier]; if (nil = cell) {cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];} cell. textLabel. text = @ 99999; // Configure the cell... return cell;} @ end

ThreeViewController. h

 

 

#import 
 
  @interface ThreeViewController : UITableViewController@end
 

ThreeViewController. m

 

 

//// ThreeViewController. m // The left and right sliding TableView /// Created by du jia on 14-1-12. // Copyright (c) June 2014 dujia. all rights reserved. // # import ThreeViewController. h @ interface ThreeViewController () @ end @ implementation ThreeViewController-(id) initWithStyle :( UITableViewStyle) style {self = [super initWithStyle: style]; if (self) {// initim 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 progress: cellIdentifier]; if (nil = cell) {cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];} cell. textLabel. text = @ 888888; return cell;} @ end

Three tableviews are created.

 

ViewController. h

 

#import 
 
  #import OneViewController.h#import TwoViewController.h#import ThreeViewController.h@interface ViewController : UIViewController
  
   @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 // The left and right sliding TableView /// Created by du jia on 14-1-12. // Copyright (c) June 2014 dujia. 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 didreceivemorywarning]; // Dispose of any resources that can be recreated .} @ end


 

 

 

 



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.