iOS: Can I have a UITableView scrolling horizontally in the iPhone landscape mode ? [duplicate]
Possible Duplicate:
Horizontal scrolling UITableView
Can I have a UITableView scrolling horizontally in the iPhone landscape mode ?
This is how it looks like now: http://cl.ly/95xZ
This is 开发者_运维问答how it should be: http://cl.ly/93IY
I was wondering if I should use a UIScrollView for the landscape mode and therefore to switch between UITableView and UIScrollView depending on the orientation of the device...
thanks
You can try this
HorizontalTable for iOS
What you need is UIScrollView with pagination. It works essentially the same way a standard UITable does. When you scroll from left to right you just push new "cells" on the stack and present them.
Here is a sample code:
// .h File
@interface PagingViewController : UIViewController <UIScrollViewDelegate>{
NSMutableArray *cells;
}
@property(nonatomic,retain)NSMutableArray *cells;
-(id)initWithCells:(NSMutableArray*)tableCells;
-(void)loadPage:(int)page;
@end
// .m File
@interface PagingViewController ()
@property(nonatomic,retain)NSMutableArray *viewControllers;
@property(nonatomic,retain)UIView *contentView;
@property(nonatomic,readwrite)NSUInteger numberOfPages;
@end
@implementation PagingViewController
@synthesize viewControllers;
@synthesize contentView;
@synthesize cells;
@synthesize numberOfPages;
-(id)initWithCells:(NSMutableArray*)tableCells{
self = [super init];
if(self) {
self.cells = tableCells;
numberOfPages = [cells count];
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < numberOfPages; i++)
{
[controllers addObject:[NSNull null]];
}
self.viewControllers = controllers;
[controllers release];
contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
self.view = contentView;
[self loadPage:0];
}
return self;
}
- (void)loadPage:(int)page{
if (page < 0)
return;
if (page >= kNumberOfPages)
return;
else if(page > 0){
CustomCellVC *cell = [viewControllers objectAtIndex:page];
if ((NSNull *)cell == [NSNull null]){
cell = (CustomCellVC*)[cells objectAtIndex:page];
[viewControllers replaceObjectAtIndex:page withObject:cell;
[cell release];
}
if (cell.view.superview == nil)
{
[contentView addSubview:cell.view];
}
}
}
//Dont forget to dealloc.
I wrote this from memory so there might be little bugz.
To implement horizontal scrolling you should use UIScrollView and manage its subviews manually. It's not very hard as it sounds. There are several videos from WWDC about technics which you can use to work with scroll view. They are worth watching.
Advanced ScrollView Techniques at https://developer.apple.com/videos/play/wwdc2011/104/ Designing Apps with Scroll Views at https://developer.apple.com/videos/play/wwdc2010/104/
Very simple example, how to show scrollView when user rotates device to landscape:
//header
// tableView and scrollView has the same superview
// scrollView is placed above tableView
@property(nonatomic, retain) IBOutlet UITableView *tableView;
@property(nonatomic, retain) IBOutlet UIScrollView *scrollView;
//implementation
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[UIView animateWithDuration:duration
animations:^(void) {
if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
self.scrollView.alpha = 1.0f;
} else {
self.scrollView.alpha = 0.0f;
}
}];
}
I would put the UITableView inside the UIScrollView. This gives you the horizontal scrolling.
But I'm a litte bit confused: The 2nd screenshot shows a UINavigationController?
精彩评论