Multiple instances of the same UIView in UIScrollView - ios

I've used this answer to implement a UIScrollView with paging.
It works great as a start, but what I want to be able to do is for each page in the UIScrollView, I want a separate instance of the same UIView.
For example, say I have ContentViewController, and I want to use 4 instances of it's UIView;
1 for each of the pages on the UIScrollView - how do I go about doing that?
My initial thought was to instantiateViewController(withIdentifier), and then reuse it's .view' property inside thefor` loop, but this only gave me the correct view once, and the other 3 were blank subviews.
Any help would be much appreciated.
Thanks!

You could instantiate the ViewController multiple times (in a for loop) and then fetch each controller's view and add it to the scroll view. But you have to be aware that now you also have 4 Controllers, each for one view.

Related

MKMapView on Storyboard on multiple Views with just 1 instance

I got a MKMapView on a ViewController in Storyboard and set constraints for it. The thing is, that i want to use one single instance (which i created programatically) on multiple ViewControllers (it is the same ViewController multiple times --> on a PageViewController).
My question is if this is working or do i have to set the constraints for the mapView programatically. If so, i don't know how to set them properly in code for my MapView, could someone help there?
Need to give it a TopSpace to the last SubView and a BottomSpace to Container. Leading and Trailing Space set to 0.
Working with Swift.
i want to use one single instance (which i created programatically) on multiple ViewControllers (it is the same ViewController multiple times --> on a PageViewController).
You can't. The same instance of a UIView cannot appear in multiple superviews. This is basically for the same reason that you cannot be in two places at once.
Instead, as one view controller's view goes away, capture the center and span of its map view and pass that info along to the next view controller, so that its map view reproduces what the user was seeing previously.

Multiple UITableViews visible at once in one UIViewController

I have seen questions asked about mutliple UITableViews in one view but they all have only one table visible at a time. I am after something different please.
In my head I want four UITableViews visible in one UIScrollView inside one UIView. The four tables will be visible and populated at once. They will have a dynamic number of rows each so the scroll view will allow users to scroll off of the page to see rows that do not fit.
The tables would be two side by side and then below them the next two side by side so that you end up with a 2x2 square.
I can (sort of) wrap my head around how to code this in the controllers etc. but I cannot figure out how to organise the hierarchi. I have tried using the storeboard to layout the tables inside the view but 9 out of 10 attempts to drop controls in fail as I am obviously not fully understanding this.
Do I need to generate the UITableViews in the UIViews implementation file and add them as objects to the UIView? Or can I use the Storyboard?
Could someone please explain how the hierarchi of objects would be structured?
In my head it would be:
UIViewController
-> UiView
---> UIScrollView
------> UITableView
------> UITableView
------> UITableView
------> UITableView
But trying this in Storyboard doesn't work. I assume each UITableView will want its own UITableViewController and what I have read in other posts I would likey need to do this connecting in the UIViewController implementation file.
Any help is appreciated.
I think you might try to drag UITableViewController into your view Controller, at least I don't have that problem to add 4 table view into a scroll view.
here is how i added it
1.> Drag the scroll view control into view controller
Your view controller should look like this:
2.> Drag the table view control into the scroll view, and set the size and position of that table view
Your view controller should look like this:
3.> Then drag all the rest 3 table views onto Scroll view
But i would like to suggest a couple of things in your case
no using that much table view in the same view controller, it's a chaos in your codes to maintain all them. There are always better
options than 4 table view, maybe consider collection view. or even
separate the use flow.
If i were you, i won't use table view inside Scroll view, they are like scroll view inside scroll view, if you don't design the
interaction very very well, they become extremely hard to use.
If you still want to use four table view in the same view controller after all, you want to pay extra attentions on your table view datasource and delegate. very carefully handle all the cases.
Hope that helps you.
Tableviews are very customized scrollviews. I wouldn't put 4 of them on a scrollview, this wouldn't be very intuitive for the user as your finger would scroll the view in many ways depending on where exactly it touches the screen.
Instead, try having your 4 tableviews in a 2x2 pattern directly onto a simple UIView. This can be done inside the Storyboard.
As for filling up and using them, you have 2 ways :
A) Your UIViewController is the delegate and datasource of each of the 4 tableviews. When executing, you perform a switch on the first parameter (the tableview that called you) to determine the appropriate course of action.
B) You create 4 classes that each handle a single tableview, instanciate 4 objects inside your UIViewController and assign the UITableviews' delegate and datasource properties to these objects.
All technicality aside, 4 tableviews in a single screen is pretty crowded. Your design will definitely not fly on a iPhone, so I'm assuming iPad only. Even then, you should make sure that everything is visually appealing and the purpose of each control is clear. It's up to you, but I'd hate to see you work hard on an application only to see your efforts wasted because your visual design doesn't appeal to your users.
If the table views take up the entire region of the scroll view then they wont let any scroll events past to the scroll view that contains them, unless the scroll is horizontal.
For a simple one to one between a table view and a view controller, I would make each table view part of it's own UITableViewController (so you have four), and then make a UIViewController that adds each of the UITableViewControllers to it as a child.
This way you don't have to do any fancy logic around if statements on which tableview is asking for data, because the table view controllers only have one table view.
https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html

UIScrollView's Frame getting changed randomly

In my view I am using a UITableView that is controlled by a UITableViewController on the top half of the screen. The remaining screen is used for a UIScrollView that contains a view that is controlled by the main UIViewController.
When I perform a pull down to refresh in the UITableViewController, (for some reason if the number of table entries is less than or greater than the initial load value, the UIScrollView in the main UIViewController's frame gets changed to the screensize...
Essentially it breaks my paging unless I reset the scrollview back to the intialized size...
I have no idea why this happens as the UIScrollView is not used in the UITableViewController. The only scrollview that is used in the UITableViewController is the UITableView's to handle pull down to refresh...
Does anyone know why the main UIScrollView's contentSize gets changed randomly when it shouldn't even been accessible from the UITableViewController class?
Thanks
Just tried it here, and I can't duplicate your experience. I'm guessing you have an unexpected or inconsistent view/controller hierarchy? Look at the controller of the table and scroll views' common superview. Anything fishy there? Remember: view controllers manage sets of views. Container view controllers manage other view controllers and have special rules (see: The View Controller Programming Guide, esp. -addChildViewController:, etc.).
I'd suggest opening a blank project and trying to recreate the problem in its simplest possible form. If it's magically fixed, what's different? If it's still giving you trouble, send us a link so we can see the details of how you have things wired.

How can I add more than one UITableView in a UIViewController?

Here is my app.
http://www.flickr.com/photos/baozhiqiang/6943921630/
http://www.flickr.com/photos/baozhiqiang/6943921900/
You can see three buttons at the top of the main screen. When you click it, the gallery will change to next. They are not the same layout style.
I created it by making three ScrollView in a big ScrollView. Code like this:
[backgroundScrollView addSubview:innerNewsScrollView];
[backgroundScrollView addSubview:innerHotScrollView];
[backgroundScrollView addSubview:innerLinkScrollView];
And when click the button, I changed the frame of the ScrollView.
Now there is a problem, when I add more than 100 Pictures(UIButton with image) to the ScrollView, It had crashed. I want to use UITableView replace ScrollView. But how can I Control more than one UITableView in a UIViewController?
Anybody can help me!
Two possibilities:
All the table view delegate methods include the table in question as one of the paramters, so it would simply be a case of checking which table is requesting data/information
You don't have to point your table views to the view controller. Your view controller could store two objects whose only purpose is to repond to table view delegate methods
Unless the tables are trivial, I think the second option is the cleanest.
Add all of your UITableViews in the UIViewController's view, set their tag properties to something you can distinct them and also set their delegates (usually self if your UIViewController implements the UITableViewDelegate protocol). Every method in the UITableViewDelegate passes UITableView reference as an argument and with the value of the tag property you can find out which table view is processed.

How to use UIViewController in non-modal way?

I'm having a uiscrollview with 3 slots, each holding a tableview with different data.
As you can imagine, handling 3 tableviews on the same viewcontroller is a bit of a mess.
So I was thinking about using multiple uiviewcontrollers to handle the different logic required for each tableview. However I can't seem to do this since uiviewcontrollers load in a modal way/blocking the entire previous view: this leaves me unable to continue to use the uiscrollview placed on my root view controller.
Is there a way to solve this? Or pack UIView logic and behavior in some sort of stub/hidden uiviewcontroller that doesn't block the view below?
You are looking at this the wrong way. Separating your tableviews is a good idea, but there is no need to use a view controller.
Let's say you have three tableViews: tableViewOne, tableViewTwo, and tabelViewThree.
The problem (as I understand it) is that all of your logic code (datasource and delegate methods) are getting all mixed up. So, just create a subclass of NSObject for each tableView and use that object for the datasource/delegate. Just make sure you set the datasource/delegate properties of the tableView to the right object.

Resources