41

I want implement a layout in my ipad application that has a uitable view that scrolls left and right rather then up and down :

So rather than

row 1 row 2 row 3 ( scrolling vertically ) It would be : row 1, row2, row 3 (scrolling horizontally )

I've seen that UItableView is designed to only do vertical scrolling so doing a transform does not give the desired effect. Is there a standard way to do this taking advantage of a datasource provider like uitableview provides?
I basically want to do somthing similar to what the BBC News reader app on the Ipad does with the list of stories to select from.

Thanks

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
imran
  • 411
  • 1
  • 4
  • 4

7 Answers7

28

I have published sample code that demonstrates one approach for implementing horizontally scrolling table views using transforms. It's called EasyTableView and provides the same interface for both vertically and horizontally scrolling table views.

aleksey
  • 413
  • 4
  • 8
26

This is the method that I use:

1) Implement you own subclass of UITableView and override its initWithCoder: method as shown below:

- (id)initWithCoder:(NSCoder *)aDecoder
{
    assert([aDecoder isKindOfClass:[NSCoder class]]);

    self = [super initWithCoder:aDecoder];

    if (self) {

        const CGFloat k90DegreesCounterClockwiseAngle = (CGFloat) -(90 * M_PI / 180.0);

        CGRect frame = self.frame;
        self.transform = CGAffineTransformRotate(CGAffineTransformIdentity, k90DegreesCounterClockwiseAngle);
        self.frame = frame;    

    }

    assert(self);
    return self;
}

2) Create your own UITableViewCell class and override initWithCoder: again:

- (id)initWithCoder:(NSCoder *)aDecoder
{
    assert([aDecoder isKindOfClass:[NSCoder class]]);

    self = [super initWithCoder:aDecoder];

    if (self) {

        const CGFloat k90DegreesClockwiseAngle = (CGFloat) (90 * M_PI / 180.0);

        self.transform = CGAffineTransformRotate(CGAffineTransformIdentity, k90DegreesClockwiseAngle);
    }

    assert(self);
    return self;
}

3) Now you can create a UITableView element in IB and set its class to be "MyHorizontalTableView" in the identity inspector.

4) Create your UITableViewCell element in IB and set its class to be "MyHorizontalTableViewCell" in the identity inspector.

And that's it.

This would work by overriding other initializers too in case you prefer not to use IB to instantiate your table view or cell.

A sample project that I built around this concept can be found in GitHub.

diegoreymendez
  • 1,997
  • 18
  • 20
  • 3
    nice but there is a Problem.... If I try to resize the table while running the App the cells won't resize vertical. The horizontal size is not a problem, because there is the heightForRow Methode from the delegate... but there is no one for the with... and after the transformation the autoresize wont work :-( – Maurice Raguse Aug 02 '12 at 15:16
  • i need to do all these 4 steps? – Omer Obaid Feb 25 '14 at 12:09
  • @OmerObaid - there are other ways to achieve the same result. You could theoretically override other initializers instead. This is just one way to do it. – diegoreymendez Feb 26 '14 at 15:02
  • @diegoreymendez : the transforming calculation didn't worked on iOS 8.1.3 (iPad) – Maulik Feb 20 '15 at 07:29
  • There is a white line on the bottom. Set `tableView.separatorStyle = UITableViewCellSeparatorStyleNone;` would remove it. – tounaobun Jun 12 '16 at 03:36
  • 1
    Excellent Solution. Neat & Clean. +1 – viral Mar 23 '17 at 08:42
  • @Raegtime To solve this, set constraints for the height within the UITableViewCell. I had the same problem and solved it with constraints – Joshua Wolff Jul 07 '19 at 20:32
  • self.heightAnchor.constraint(equalToConstant: 200).isActive = true changes the "height" of the cell, which is really the width – Joshua Wolff Jul 07 '19 at 20:48
  • Overall though, would like to mention that having used this, it works almost as good as a solution built-in by Apple, if there were one – Joshua Wolff Jul 07 '19 at 20:57
10

If you can restrict your app to only iOS 6 and upwards, the best way to do this is with UICollectionView.

If you need to support iOS 4/5, then try one of the opensource reimplementations of UICollectionView, eg. PSTCollectionView

JosephH
  • 37,173
  • 19
  • 130
  • 154
  • Can you give me some hints (links or code) on how to do this. I guess I have to play with `UICollectionViewLayout`. Any help appreciated. Thanks! – Colas May 28 '14 at 10:41
  • You're correct; you'd use a Grid Layout with a single row. – JosephH May 28 '14 at 15:08
  • How can I create a single-row Grid Layout? So far, I use a trick: I ask for very large minimal space between two elements. – Colas May 28 '14 at 16:29
5

This question has been answered by apple. Scrolling example by apple

Demonstrates how to implement two different style UIScrollViews. The first scroller contains multiple images, showing how to layout large content with multiple chunks of data (in our case 5 separate UIImageViews).

this works for iPad also.

Saqib Saud
  • 2,799
  • 2
  • 27
  • 41
  • 3
    Dead resource link :( – ULazdins Dec 20 '16 at 07:37
  • 1
    that sample code was way too old. you can still access it https://web-beta.archive.org/web/20100805074840/http://developer.apple.com:80/iphone/library/samplecode/Scrolling/Listings/ReadMe_txt.html – Saqib Saud Dec 20 '16 at 12:56
3

For now you have to use UIScrollView and set scrolling to horizontal only. As for the data you need handle the dequeing and optimizations yourself. If you do not expect more than a dozen or so objects then you can probably skip it, but if they are data intensive try to implement a similar data sourcing and loading as UITableView uses. Note that the BBC News reader app also uses paging enabled to scroll by each 'page (4 or so news icons)'.

hardway
  • 31
  • 1
1

I wrote a simple UIScrollView subclass that like UITableView implement cell reusability, the projet MMHorizontalListView is on gitHub, there is also a test project, so you can see how to use it with an example, it works also with old iOS versions like 4.x (probably even older...)

Manu
  • 788
  • 5
  • 10
1

There is a simple brilliant trick to get a UITableView to do both vertical and horizontal scroll perfectly.

You can do it both with and without autolayout - I will explain it with autolayout.

On you UITableView have a width constraint and no tailing alignment constraint. The bind this width constraint with an IBOutlet and in your controller set the following to properties on the table view.

let contentWidth = 1000 // you calculate that
tableViewWidthConstraint.constant = contentWidth
tableView.contentSize.width = (contentWidth * 2) - UIScreen.main.bounds.size.width

The tableview needs to be full width to render all the content in the horizontal direction, and the we plays with the content size compared with screen size that does the trick.

The following locations of them below works but you can try the in different steps in the lifecycle:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    tableViewWidthConstraint.constant = contentWidth
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    tableView.contentSize.width = (contentWidth * 2) - UIScreen.main.bounds.size.width
}

// and this is needed to support rotation:
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        coordinator.animate(alongsideTransition: { (UIViewControllerTransitionCoordinatorContext) -> Void in
            self.tableView.contentSize.width = (contentWidth * 2) - UIScreen.main.bounds.size.width
        }, completion: nil)
    }
    super.viewWillTransition(to: size, with: coordinator)
}
Morten Holmgaard
  • 7,484
  • 8
  • 63
  • 85