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

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.

Related

UITableView inside a UITableView?

Generally:
Is it OK to add a UITableView as a subview of another UITableView? Or, should I create a UIView and add each UITableView to it?
Specifically:
For a ComposeTableViewController with a typeahead, like in the iPhone's native Mail app, which approach would you recommend and why?
Note: I prefer to construct things 100% programmatically (no Interface Builder).
Subclass UITableViewController.
Then, to show the typeahead results, create and add a resultsTableView as a subview of self.tableView positioned directly underneath the the cell (of self.tableView) with the typeahead text field.
The nice thing about this approach is that resultsTableView scrolls with self.tableView automatically.
But, is it OK to add a UITableView as a subview of another UITableView?
Subclass UIViewController.
Create and add tableView (custom property) as a subview of self.view.
Create and add resultsTableView also as a subview of self.view.
The annoying thing about this approach is that I have to reposition resultsTableView manually anytime self.tableView scrolls.
I think I'd prefer approach 1, but adding a UITableView as a subview of another UITableView just seems smelly to me.
TableViews cannot have subviews. You can try adding a tableview as the view of a TableViewCell, but then you have to ask yourself how it would scroll, if you tried scrolling in the subtableview would it scroll the child tableview or the parent tableview? It is much easier to present multiple tableviews within a view. This can be done easily by setting your custom viewcontroller as the datasource of both tableviews contained within its view and then comparing the tableview pointer that is sent as a parameter of the datasource method to the two tableview pointers that are IVars of your custom view controller.
Hold stuff like this in a container. Create a container view controller which only does typeahead. This container view controller will hold your main table view controller (and its table view) and it will hold the typeahead view controller (and its view).
It will keep the logic needed for the typeahead out of your main table view and as a bonus you might end up with a reusable typeahead container which could be applied to other views.
Yes it is good to go for adding UITableView in as a cell of another UITableView.
I had one issue and posted a question
Multiple Views as subviews
And requirement was like a group of controls with list of other controls. I thought that time that it will be messy if i'm going to add UITableView as a cell of UITableView.
What i found that... Boy !! it's how iOS meant to be. There is no any lag while doing this.
Tested for below functionalities:
Scrolls like a charm
Separate delegates called for each
Reordering of both UITableView cell
Swipe and remove cell
Runtime datasource update for both UITableView and reload both at the same time.
There is no any reason not to add subview UITableView.
You can do it but it is against the principle of MVC. You will be setting the delegate of a view to another view which is wrong.

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

Resizing UIPopoverController according to number of ChildViewControllers

I am trying to create a popover that looks like the below
Basically, it has three main sections - a header which has some labels in it, a body and a footer which has 2 buttons. The body is made up of a variable number of ChildViewControllers.
Question
1) how should I go about doing this? I am thinking in viewWillAppear I call a web service which asynchronously returns me the number of ChildViewControllers to create. In the callback method I then create the ChildViewControllers, put them into an array and also add them as ChildViewControllers using addChildViewController. Will it be a problem adding ChildViewControllers outside of viewDidLoad or viewWillAppear etc and in the callback method?
2) this view controller is going to appear in a popover. how can I resize the popover in the view controller such that I still get to keep my header and footer (essentially only the centre portion with the varying number of childViewControllers gets adjusted.
in the end I implemented this using a UITableViewController. The header is a UIView implemented as a table header. While the two buttons are put into a UIView as the table footer. ChildViewControllers are put into UITableViewCells. I then disabled scrolling on the UITableView.
This method made it very easy to achieve the variable height of the center portion while retaining header and footer.

Is it possible to have UIpageviewcontroller and UItableview on the same screen in ios?

i m new to iphone development i m doing an application where i want a uipageviewcontroller to display company images and below that i need a tableview to display a list like services,company profile etc..Issue i m facing here is for UIpageviewcontroller it has to make a link with file owner view and for tableview also it has to link with file owner view but both cannot be possible at the same time because there was only one view controller.so how i need to implement this?
Any suggestions please
Thanks
Edit: Slightly modified approach: Since you want to mix multiple views on the "same page" (which generally should mean within a single parent UIViewController), you do not want a UIPageViewController and a UITableViewController since each of these wants to own the entire screen. Instead, you want a UIScrollView + UIPageControl to simulate the UIPageViewController, and a UITableView, all within a single UIViewController. These will be sub-views of your UIViewController's view and properties within your UIViewController's class. You then hook up the delegate methods of the UIScrollView, UIPageControl, and UITableView (datasource and delegate) to methods within your UIViewController class implementation.

only one section header

I'd like to have the section header (for an UITableView) for the uppermost cell only , a sort of header for the table that sticks to the top, showing some additional information about the uppermost cell.
is it somehow possible? if not (as I suppose since I've carefully read all the documentation) do you have any idea how to replicate this behaviour?
You need to create your own view to use as the header. It will be simplest to make your custom header view be a sibling of the table view, and position it so that it's above the table view on the screen.
Since UITableView is a subclass of UIScrollView, your table view's delegate is also a scroll view delegate and receives the UIScrollViewDelegate messages. You want to implement this method:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
Each time your delegate receives that message, you want it to look at which table view row is at the top of the table view (using [tableView indexPathForRowAtPoint:tableView.contentOffset]) and update the contents of your custom header view accordingly.
Read the Apple documentation for UITableView. The property you're looking for is tableHeaderView.
This is hard (not impossible) with UITableViewController because it forces the tableView to be the root view.
If you implement your own controller, inherit from UIViewController instead of UITableViewController.
You must adopt the data source and delegate protocols, and implement the methods appropriately, but then you have a normal view as your root view. You can then add a UITableView in any location and size you want, with anything you want around it.
The only real restriction is static table views you build in IB. However, in that case, you can implement view controller containment, and just parent your table view controller into another controller, and give it a specific view to take over.
The first option is dead simple, but the second is an advanced technique, and you need to understand view controller containment to do it right.

Resources