|
| 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 |
0 commit comments