4

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

Community
  • 1
  • 1
aneuryzm
  • 63,052
  • 100
  • 273
  • 488

4 Answers4

2

You can try this

HorizontalTable for iOS

ArunGJ
  • 2,685
  • 21
  • 27
1

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.

Cyprian
  • 9,423
  • 4
  • 39
  • 73
  • So, you are suggesting to use UIScrollView for the portrait mode as well ? In other words, to not use UITableView at all ? – aneuryzm Aug 07 '11 at 10:28
  • No, I would use UITableView for the Portrait and then pagination for the horizontal. I gave you an example of how you can transform regular UITableView to paging by passing an array of cells. – Cyprian Aug 07 '11 at 11:40
  • Ok, so I should handle the rotation of the device from the UITableView in the didRotate method ? I check if the device rotates and I initialize the UIScrollView if not already initialized and switch between the two views ? – aneuryzm Aug 07 '11 at 11:53
0

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;
                         }
                     }];
}
Jared Khan
  • 333
  • 2
  • 12
Evgeny Shurakov
  • 6,062
  • 2
  • 28
  • 22
  • So, should I switch between UITableView and UIScrollView or should I use UIScrollView for portrait mode as well ? I'm talking in terms of efficiency.. – aneuryzm Aug 07 '11 at 10:24
  • @Patrick, yes, you need to switch between them when the user rotate your device. Doing that you can present your information differently in portrait and landscape modes, which I think is good, because UITableView in landscape mode looks not so great. – Evgeny Shurakov Aug 07 '11 at 10:41
  • OK. So how should I exactly implement this ? Should I create a UIScrollView and set its view to my current view from didRotateFromInterfaceOrientation ? – aneuryzm Aug 07 '11 at 11:11
  • In other terms, should the UIScrollView be declared inside my UITableView and then eventually assign the current view when the device rotates ? – aneuryzm Aug 07 '11 at 11:12
  • You can just place one view above another when you device rotates. Take a look at sample code: http://developer.apple.com/library/ios/#samplecode/AlternateViews/Introduction/Intro.html – Evgeny Shurakov Aug 07 '11 at 11:26
  • So the suggested way is to use presentModalViewController ? Because the code you sent me doesn't work if I would just assign the view.. – aneuryzm Aug 07 '11 at 11:38
  • That sample code is just an example how you could do that. It is written by apple and just works. I wouldn't just assign the view, because that way you can't perform animated transition between views. If you don't want to use `presentModalViewController` method, add/remove scrollView (scrollView should be placed above your tableView) in `willRotateToInterfaceOrientation:duration:` method. I think it will work too. – Evgeny Shurakov Aug 07 '11 at 11:57
  • for add/remove scrollView you mean add it as child of the UITableview ? – aneuryzm Aug 07 '11 at 11:58
  • No, there will be no use of it, if you place scrollView inside tableView. – Evgeny Shurakov Aug 07 '11 at 12:07
  • Sorry, I don't understand how exactly should I switch to the horizontal UIScrollView. You said that assigning self.view = scrollview is not good because I don't have transitions. Then adding it as child also would be not good because it would scroll. So how should I add/remove the horizontal view ? – aneuryzm Aug 07 '11 at 12:13
  • Thanks, but what's the exact code to add the scrollView.. did you use [[self.view superview] addSubview:scrollView]; or [self.parentViewController.view addSubview:scrollView]; ? or What ? I've also tried to add the UIScrollView in the xib file but I can't add it to the main View because there is the UITableView already there. – aneuryzm Aug 08 '11 at 13:31
  • In all cases the scrollView is never visible: the UITableView is always visible. – aneuryzm Aug 08 '11 at 13:34
  • 1
    Use UIView as main view and place table view and scroll view inside it. http://dl.dropbox.com/u/167184/test-project.zip – Evgeny Shurakov Aug 08 '11 at 14:06
  • Finally, thanks god, thank you – aneuryzm Aug 08 '11 at 15:06
  • You should really be using a horizontal scrolling `UICollectionView`.... – random Apr 04 '16 at 13:44
-2

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?

Fabio Poloni
  • 8,219
  • 5
  • 44
  • 74
  • The 2nd screenshots show how a cell of the table should be displayed in horizontal mode. If you swipe horizontally the next cell is shown. I'm probably going for an UIScrollView with page segmentation... – aneuryzm Aug 07 '11 at 09:25
  • However, I don't understand what you wrote. The UITableView is already a UIScrollView... and I just need to use one of the two, right ? – aneuryzm Aug 07 '11 at 09:26
  • Is efficient if I switch between them when I rotate device or it is too expensive ? – aneuryzm Aug 07 '11 at 09:27
  • You should not embed a UITableView in a UIScrollView http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIScrollView_Class/Reference/UIScrollView.html See the first grey box. – Abizern Aug 07 '11 at 10:02
  • @Abizern Thanks. Forgot that the UITableView is already a UIScrollView... – Fabio Poloni Aug 07 '11 at 10:35