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 t开发者_开发技巧o 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.
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);
...
There's a project here that might be of some use as well, http://github.com/TheVole/HorizontalTable
EasyTableView can do horizontal tables
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;
}
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?
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.
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.
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.
I recommend EasyTableView and PSTCollectionView, they are great horizontal tableview project on github that are stared a lot.
精彩评论