Skip to content

Commit 5410123

Browse files
committed
Added HorizontalScrollableUITableView
1 parent 130c626 commit 5410123

12 files changed

+1645
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// HorizontalScrollableUITableViewAppDelegate.h
3+
// HorizontalScrollableUITableView
4+
//
5+
// Created by Hiroshi Hashiguchi on 10/10/19.
6+
// Copyright 2010 . All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
11+
@class HorizontalScrollableUITableViewViewController;
12+
13+
@interface HorizontalScrollableUITableViewAppDelegate : NSObject <UIApplicationDelegate> {
14+
UIWindow *window;
15+
HorizontalScrollableUITableViewViewController *viewController;
16+
}
17+
18+
@property (nonatomic, retain) IBOutlet UIWindow *window;
19+
@property (nonatomic, retain) IBOutlet HorizontalScrollableUITableViewViewController *viewController;
20+
21+
@end
22+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
//
2+
// HorizontalScrollableUITableViewAppDelegate.m
3+
// HorizontalScrollableUITableView
4+
//
5+
// Created by Hiroshi Hashiguchi on 10/10/19.
6+
// Copyright 2010 . All rights reserved.
7+
//
8+
9+
#import "HorizontalScrollableUITableViewAppDelegate.h"
10+
#import "HorizontalScrollableUITableViewViewController.h"
11+
12+
@implementation HorizontalScrollableUITableViewAppDelegate
13+
14+
@synthesize window;
15+
@synthesize viewController;
16+
17+
18+
#pragma mark -
19+
#pragma mark Application lifecycle
20+
21+
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
22+
23+
// Override point for customization after application launch.
24+
25+
// Add the view controller's view to the window and display.
26+
[window addSubview:viewController.view];
27+
[window makeKeyAndVisible];
28+
29+
return YES;
30+
}
31+
32+
33+
- (void)applicationWillResignActive:(UIApplication *)application {
34+
/*
35+
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.
36+
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.
37+
*/
38+
}
39+
40+
41+
- (void)applicationDidEnterBackground:(UIApplication *)application {
42+
/*
43+
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.
44+
If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
45+
*/
46+
}
47+
48+
49+
- (void)applicationWillEnterForeground:(UIApplication *)application {
50+
/*
51+
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.
52+
*/
53+
}
54+
55+
56+
- (void)applicationDidBecomeActive:(UIApplication *)application {
57+
/*
58+
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.
59+
*/
60+
}
61+
62+
63+
- (void)applicationWillTerminate:(UIApplication *)application {
64+
/*
65+
Called when the application is about to terminate.
66+
See also applicationDidEnterBackground:.
67+
*/
68+
}
69+
70+
71+
#pragma mark -
72+
#pragma mark Memory management
73+
74+
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
75+
/*
76+
Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
77+
*/
78+
}
79+
80+
81+
- (void)dealloc {
82+
[viewController release];
83+
[window release];
84+
[super dealloc];
85+
}
86+
87+
88+
@end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// HorizontalScrollableUITableViewViewController.h
3+
// HorizontalScrollableUITableView
4+
//
5+
// Created by Hiroshi Hashiguchi on 10/10/19.
6+
// Copyright 2010 . All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
11+
@interface HorizontalScrollableUITableViewViewController : UIViewController <UIScrollViewDelegate, UITableViewDelegate, UITableViewDataSource> {
12+
13+
14+
UIScrollView* scrollView_;
15+
16+
NSInteger contentOffsetIndex_;
17+
NSInteger currentViewIndex_;
18+
19+
}
20+
@property (nonatomic, retain) IBOutlet UIScrollView* scrollView;
21+
@property (nonatomic, assign) NSInteger contentOffsetIndex;
22+
@property (nonatomic, assign) NSInteger currentViewIndex;
23+
24+
- (IBAction)movePrevious:(id)sender;
25+
- (IBAction)moveNext:(id)sender;
26+
27+
@end
28+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
//
2+
// HorizontalScrollableUITableViewViewController.m
3+
// HorizontalScrollableUITableView
4+
//
5+
// Created by Hiroshi Hashiguchi on 10/10/19.
6+
// Copyright 2010 . All rights reserved.
7+
//
8+
9+
#import "HorizontalScrollableUITableViewViewController.h"
10+
#import "InnerView.h"
11+
12+
13+
#define SPACE_WIDTH 40
14+
15+
@implementation HorizontalScrollableUITableViewViewController
16+
17+
@synthesize scrollView = scrollView_;
18+
@synthesize currentViewIndex = currentViewIndex_;
19+
@synthesize contentOffsetIndex = contentOffsetIndex_;
20+
21+
/*
22+
// The designated initializer. Override to perform setup that is required before the view is loaded.
23+
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
24+
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
25+
// Custom initialization
26+
}
27+
return self;
28+
}
29+
*/
30+
31+
/*
32+
// Implement loadView to create a view hierarchy programmatically, without using a nib.
33+
- (void)loadView {
34+
}
35+
*/
36+
37+
38+
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
39+
40+
- (void)viewDidLoad {
41+
[super viewDidLoad];
42+
43+
// setup scrollview
44+
self.scrollView.delegate = self;
45+
self.scrollView.pagingEnabled = YES;
46+
self.scrollView.showsHorizontalScrollIndicator = NO;
47+
self.scrollView.showsVerticalScrollIndicator = NO;
48+
self.scrollView.scrollsToTop = NO;
49+
50+
CGRect innerViewFrame = self.scrollView.bounds;
51+
52+
CGRect scrollViewFrame = self.scrollView.frame;
53+
scrollViewFrame.origin.x -= SPACE_WIDTH/2;
54+
scrollViewFrame.size.width += SPACE_WIDTH;
55+
self.scrollView.frame = scrollViewFrame;
56+
57+
for (int i=0; i < 3; i++) {
58+
innerViewFrame.origin.x += SPACE_WIDTH/2;
59+
60+
InnerView* innerView = [[InnerView alloc] initWithFrame:innerViewFrame];
61+
innerView.dataSource = self;
62+
[self.scrollView addSubview:innerView];
63+
[innerView release];
64+
65+
innerViewFrame.origin.x += innerViewFrame.size.width;
66+
innerViewFrame.origin.x += SPACE_WIDTH/2;
67+
}
68+
69+
self.scrollView.contentSize = CGSizeMake(scrollViewFrame.size.width*3, scrollViewFrame.size.height);
70+
}
71+
72+
73+
/*
74+
// Override to allow orientations other than the default portrait orientation.
75+
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
76+
// Return YES for supported orientations
77+
return (interfaceOrientation == UIInterfaceOrientationPortrait);
78+
}
79+
*/
80+
81+
- (void)didReceiveMemoryWarning {
82+
// Releases the view if it doesn't have a superview.
83+
[super didReceiveMemoryWarning];
84+
85+
// Release any cached data, images, etc that aren't in use.
86+
}
87+
88+
- (void)viewDidUnload {
89+
// Release any retained subviews of the main view.
90+
// e.g. self.myOutlet = nil;
91+
}
92+
93+
94+
- (void)dealloc {
95+
[super dealloc];
96+
}
97+
98+
99+
100+
#pragma mark -
101+
#pragma mark UIScrollViewDelegate
102+
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
103+
{
104+
NSLog(@"scrollViewDidScroll:");
105+
return;
106+
107+
CGFloat position = scrollView.contentOffset.x / scrollView.bounds.size.width;
108+
CGFloat delta = position - (CGFloat)self.currentViewIndex;
109+
110+
if (fabs(delta) >= 1.0f) {
111+
// NSLog(@"%f (%d=>%d)", delta, self.currentViewIndex, index);
112+
113+
if (delta > 0) {
114+
// the current page moved to right
115+
self.currentViewIndex = self.currentViewIndex+1;
116+
self.contentOffsetIndex = self.contentOffsetIndex+1;
117+
118+
} else {
119+
// the current page moved to left
120+
self.currentViewIndex = self.currentViewIndex-1;
121+
self.contentOffsetIndex = self.contentOffsetIndex-1;
122+
}
123+
124+
InnerView* innerView = [self.scrollView.subviews objectAtIndex:self.currentViewIndex];
125+
[innerView reloadData];
126+
}
127+
128+
}
129+
130+
131+
132+
#pragma mark -
133+
#pragma mark UITableViewDataSource
134+
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
135+
{
136+
return 10;
137+
}
138+
139+
140+
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
141+
142+
static NSString *CellIdentifier = @"cell";
143+
144+
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
145+
if (cell == nil) {
146+
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
147+
reuseIdentifier:CellIdentifier] autorelease];
148+
}
149+
150+
cell.textLabel.text = [NSString stringWithFormat:@"currentIndex:%d", self.currentViewIndex];
151+
152+
return cell;
153+
}
154+
155+
156+
157+
#pragma mark -
158+
#pragma mark UITableViewDelegate
159+
160+
161+
#pragma mark -
162+
#pragma mark Event
163+
- (void)scroll
164+
{
165+
CGPoint offset =
166+
CGPointMake(self.scrollView.bounds.size.width * self.currentViewIndex, 0);
167+
self.scrollView.contentOffset = offset;
168+
InnerView* innerView = [self.scrollView.subviews objectAtIndex:self.currentViewIndex];
169+
[innerView reloadData];
170+
}
171+
172+
- (IBAction)movePrevious:(id)sender
173+
{
174+
if (self.currentViewIndex == 0) {
175+
return;
176+
}
177+
self.currentViewIndex -= 1;
178+
[self scroll];
179+
}
180+
181+
- (IBAction)moveNext:(id)sender
182+
{
183+
if (self.currentViewIndex >= 2) {
184+
return;
185+
}
186+
self.currentViewIndex += 1;
187+
188+
[self scroll];
189+
}
190+
191+
@end

Diff for: HorizontalScrollableUITableView/Classes/InnerView.h

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// InnerView.h
3+
// HorizontalScrollableUITableView
4+
//
5+
// Created by Hiroshi Hashiguchi on 10/10/19.
6+
// Copyright 2010 . All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
12+
@interface InnerView : UITableView {
13+
14+
}
15+
16+
@end

Diff for: HorizontalScrollableUITableView/Classes/InnerView.m

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// InnerView.m
3+
// HorizontalScrollableUITableView
4+
//
5+
// Created by Hiroshi Hashiguchi on 10/10/19.
6+
// Copyright 2010 . All rights reserved.
7+
//
8+
9+
#import "InnerView.h"
10+
11+
12+
@implementation InnerView
13+
14+
15+
- (id)initWithFrame:(CGRect)frame
16+
{
17+
if (self = [super initWithFrame:frame style:UITableViewStylePlain]) {
18+
}
19+
return self;
20+
}
21+
22+
@end

0 commit comments

Comments
 (0)