I'm trying to implement a table view with scrollable horizontal items (with unknown number of items) like the illustration below:
The UI should behave like so:
I thought about using TableView and dequeueReusableCellWithIdentifier
and creating a prototype cell, but then I need to remember the list horizontal position and init the cell properly on cellForRowAtIndexPath
and that will probably affect performance.
Q: What layout would you use in order to achieve this goal, any input / tutorial would be appreciated
You can add a UITableView to a UIScrollView and have it scroll horizontally.
You need to set the UITableView scroll to false , tableview. scrollEnabled = false; tableview.
To scroll to the top of our tableview we need to create a new IndexPath . This index path has two arguments, row and section . All we want to do is scroll to the top of the table view, therefore we pass 0 for the row argument and 0 for the section argument. UITableView has the scrollToRow method built in.
Use a UITableView
and add a UICollectionView
to your reusable tableView cell. UICollectionView
works similar to UITableView
in that the "items" (like cells) are reusable and instantiated only when they will appear on screen. I did a quick google search for tutorials and found UICollectionView in UITableViewCell. Check that out and a few other StackOverflow questions regarding design and you should be golden.
Using UIScrollView
may require heavy effort if there are large amount of UI elements inside each scrollview because you have to instantiate all those upfront. A better solution is to define your custom UITableViewCell
which has its own UITableView
but rotated in 90 degree for horizontal scrolling. UI elements inside will be created only when they need to be shown by using dequeueReusableCellWithIdentifier:forIndexPath:
. Please see some sample code on how to create a 90 degree rotated table view:
iPhone Tableview Use Cells in Horizontal Scrolling not Vertical
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With