Skip to content

Commit

Permalink
Added HorizontalScrollableUITableView
Browse files Browse the repository at this point in the history
  • Loading branch information
xcatsan committed Oct 20, 2010
1 parent 130c626 commit 5410123
Show file tree
Hide file tree
Showing 12 changed files with 1,645 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// HorizontalScrollableUITableViewAppDelegate.h
// HorizontalScrollableUITableView
//
// Created by Hiroshi Hashiguchi on 10/10/19.
// Copyright 2010 . All rights reserved.
//

#import <UIKit/UIKit.h>

@class HorizontalScrollableUITableViewViewController;

@interface HorizontalScrollableUITableViewAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
HorizontalScrollableUITableViewViewController *viewController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet HorizontalScrollableUITableViewViewController *viewController;

@end

Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
//
// HorizontalScrollableUITableViewAppDelegate.m
// HorizontalScrollableUITableView
//
// Created by Hiroshi Hashiguchi on 10/10/19.
// Copyright 2010 . All rights reserved.
//

#import "HorizontalScrollableUITableViewAppDelegate.h"
#import "HorizontalScrollableUITableViewViewController.h"

@implementation HorizontalScrollableUITableViewAppDelegate

@synthesize window;
@synthesize viewController;


#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// Override point for customization after application launch.

// Add the view controller's view to the window and display.
[window addSubview:viewController.view];
[window makeKeyAndVisible];

return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
/*
Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
*/
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
/*
Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
*/
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
/*
Called as part of transition from the background to the inactive state: here you can undo many of the changes made on entering the background.
*/
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
/*
Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
*/
}


- (void)applicationWillTerminate:(UIApplication *)application {
/*
Called when the application is about to terminate.
See also applicationDidEnterBackground:.
*/
}


#pragma mark -
#pragma mark Memory management

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
/*
Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
*/
}


- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}


@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// HorizontalScrollableUITableViewViewController.h
// HorizontalScrollableUITableView
//
// Created by Hiroshi Hashiguchi on 10/10/19.
// Copyright 2010 . All rights reserved.
//

#import <UIKit/UIKit.h>

@interface HorizontalScrollableUITableViewViewController : UIViewController <UIScrollViewDelegate, UITableViewDelegate, UITableViewDataSource> {


UIScrollView* scrollView_;

NSInteger contentOffsetIndex_;
NSInteger currentViewIndex_;

}
@property (nonatomic, retain) IBOutlet UIScrollView* scrollView;
@property (nonatomic, assign) NSInteger contentOffsetIndex;
@property (nonatomic, assign) NSInteger currentViewIndex;

- (IBAction)movePrevious:(id)sender;
- (IBAction)moveNext:(id)sender;

@end

Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
//
// HorizontalScrollableUITableViewViewController.m
// HorizontalScrollableUITableView
//
// Created by Hiroshi Hashiguchi on 10/10/19.
// Copyright 2010 . All rights reserved.
//

#import "HorizontalScrollableUITableViewViewController.h"
#import "InnerView.h"


#define SPACE_WIDTH 40

@implementation HorizontalScrollableUITableViewViewController

@synthesize scrollView = scrollView_;
@synthesize currentViewIndex = currentViewIndex_;
@synthesize contentOffsetIndex = contentOffsetIndex_;

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
}
return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.

- (void)viewDidLoad {
[super viewDidLoad];

// setup scrollview
self.scrollView.delegate = self;
self.scrollView.pagingEnabled = YES;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.showsVerticalScrollIndicator = NO;
self.scrollView.scrollsToTop = NO;

CGRect innerViewFrame = self.scrollView.bounds;

CGRect scrollViewFrame = self.scrollView.frame;
scrollViewFrame.origin.x -= SPACE_WIDTH/2;
scrollViewFrame.size.width += SPACE_WIDTH;
self.scrollView.frame = scrollViewFrame;

for (int i=0; i < 3; i++) {
innerViewFrame.origin.x += SPACE_WIDTH/2;

InnerView* innerView = [[InnerView alloc] initWithFrame:innerViewFrame];
innerView.dataSource = self;
[self.scrollView addSubview:innerView];
[innerView release];

innerViewFrame.origin.x += innerViewFrame.size.width;
innerViewFrame.origin.x += SPACE_WIDTH/2;
}

self.scrollView.contentSize = CGSizeMake(scrollViewFrame.size.width*3, scrollViewFrame.size.height);
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}


- (void)dealloc {
[super dealloc];
}



#pragma mark -
#pragma mark UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
NSLog(@"scrollViewDidScroll:");
return;

CGFloat position = scrollView.contentOffset.x / scrollView.bounds.size.width;
CGFloat delta = position - (CGFloat)self.currentViewIndex;

if (fabs(delta) >= 1.0f) {
// NSLog(@"%f (%d=>%d)", delta, self.currentViewIndex, index);

if (delta > 0) {
// the current page moved to right
self.currentViewIndex = self.currentViewIndex+1;
self.contentOffsetIndex = self.contentOffsetIndex+1;

} else {
// the current page moved to left
self.currentViewIndex = self.currentViewIndex-1;
self.contentOffsetIndex = self.contentOffsetIndex-1;
}

InnerView* innerView = [self.scrollView.subviews objectAtIndex:self.currentViewIndex];
[innerView reloadData];
}

}



#pragma mark -
#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)table 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 = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}

cell.textLabel.text = [NSString stringWithFormat:@"currentIndex:%d", self.currentViewIndex];

return cell;
}



#pragma mark -
#pragma mark UITableViewDelegate


#pragma mark -
#pragma mark Event
- (void)scroll
{
CGPoint offset =
CGPointMake(self.scrollView.bounds.size.width * self.currentViewIndex, 0);
self.scrollView.contentOffset = offset;
InnerView* innerView = [self.scrollView.subviews objectAtIndex:self.currentViewIndex];
[innerView reloadData];
}

- (IBAction)movePrevious:(id)sender
{
if (self.currentViewIndex == 0) {
return;
}
self.currentViewIndex -= 1;
[self scroll];
}

- (IBAction)moveNext:(id)sender
{
if (self.currentViewIndex >= 2) {
return;
}
self.currentViewIndex += 1;

[self scroll];
}

@end
16 changes: 16 additions & 0 deletions HorizontalScrollableUITableView/Classes/InnerView.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// InnerView.h
// HorizontalScrollableUITableView
//
// Created by Hiroshi Hashiguchi on 10/10/19.
// Copyright 2010 . All rights reserved.
//

#import <Foundation/Foundation.h>


@interface InnerView : UITableView {

}

@end
22 changes: 22 additions & 0 deletions HorizontalScrollableUITableView/Classes/InnerView.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// InnerView.m
// HorizontalScrollableUITableView
//
// Created by Hiroshi Hashiguchi on 10/10/19.
// Copyright 2010 . All rights reserved.
//

#import "InnerView.h"


@implementation InnerView


- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame style:UITableViewStylePlain]) {
}
return self;
}

@end
Loading

0 comments on commit 5410123

Please sign in to comment.