Tattat
Tattat

Reputation: 15778

How to make a horizontal UI table view on iPhone?

Normally, the UITableView is something like this:

[   cell    1   ]
[   cell    2   ]
[   cell    3   ]
[   cell    4   ]

But I want to make my own UITableView like this:

   |    |   |   |
   | c  | c | c |
   | e  | e | e |
   | l  | l | l |
   | l  | l | l |
   |    |   |   |
   | 1  | 2 | 3 |
   |    |   |   |

And I want the user swipe left and right to have the similar behavior like the original UITableView.... ...How can I do this? thank you.

Upvotes: 19

Views: 26817

Answers (9)

Alex
Alex

Reputation: 2150

if you want use textLabel, you can use this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:CellIdentifier];
    }


    cell.textLabel.frame = CGRectMake(0,0, cell.frame.size.width , cell.frame.size.width); // it's very important!!!You must set frame of textLabel, because you can have a problems with displaying you table;

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    CGAffineTransform trans = CGAffineTransformRotate(CGAffineTransformIdentity, M_PI_2);
    cell.textLabel.transform = trans;  // rotating you caption;
    cell.textLabel.text = @"tra-tata";
    cell.textLabel.textAlignment = UITextAlignmentCenter;

    return cell;
}

Upvotes: 2

jianpx
jianpx

Reputation: 3320

I recommend EasyTableView and PSTCollectionView, they are great horizontal tableview project on github that are stared a lot.

Upvotes: 0

gabrielpalomino
gabrielpalomino

Reputation: 321

Use a UIViewController with a UIView, add the UITableView of your table to the controller view, then in terms of code is just one line:

- (void)viewDidLoad
{

    [super viewDidLoad];

    // horizontal table
    // -90 degrees rotation will move top of your tableview to the left
    myTableView.transform = CGAffineTransformMakeRotation(-M_PI_2);
    //Just so your table is not at a random place in your view
    myTableView.frame = CGRectMake(0,0, myTableView.frame.size.width, myTableView.frame.size.height);
...

Upvotes: 21

neoneye
neoneye

Reputation: 52161

EasyTableView can do horizontal tables

Upvotes: 4

Colin Barrett
Colin Barrett

Reputation: 4451

It isn't too terribly difficult to build up something similar to UITableView in code on your own. If you can find it, the expert-level Scroll Views session from WWDC09 had some awesome sample code.

Upvotes: 0

Tom Irving
Tom Irving

Reputation: 10059

There's a project here that might be of some use as well, http://github.com/TheVole/HorizontalTable

Upvotes: 13

gnasher
gnasher

Reputation: 1513

What you have is simply a standard table displayed in portrait orientation, but your are holding the phone in landscape mode. Rotate the contents of each cell 90 degrees and your there. And be sure to ignore any orientation change notifications.

Upvotes: 0

MrHen
MrHen

Reputation: 2480

Other than subclassing UIScrollView, you could try applying a 90 degree rotation to an existing UITableView. I have no idea how that will affect scrolling, however, and you would have to specialize the cell views to "unrotate."

I think subclassing UIScrollView is a better option. Use the UITableView class as a guideline when it comes to the delegate, dataSource, and reusable cells.

Upvotes: 0

pheelicks
pheelicks

Reputation: 7469

I don't think there is a available control which will enable you to do this. Your best bet is to roll your own.

In a nutshell you'll want to subclass UIScrollView, which will allow you to have more cells than you have room for on screen. It will also sort out all your swiping behaviour.

Into this scroll view you want to put a set of cells - I'd subclass UIView rather than trying to force UITableViewCell play nice with your Scroll View for these.

This is just a simple example of how to approach the problem. How many of the features of UITableView are you hoping to replicate?

Upvotes: 1

Related Questions