UITableView with dynamically set size - iphone

I have a UITableView as a subview in a ScrollView with other widgets around like a button. I'd like to put the button always at the end of the ScrollView and I'd like to have the UITableView to show dynamically more section. How and where shall I determine the Table size, correctly set it and visualize it?
From Interface Builder it seems that I can only set static size to the TableView (which of course limits the number of sections visible) and stick the button position to the bottom whether a rotation happens.

If you have only few simple controls after the table then I'd suggest putting them to the table itself and get rid of the unhealthy (in my opinion) combination of table view inside scroll view.
You might add the button you are talking about to the table's footer.
It may be done in the Interface Builder (drag-n-drop the button to the bottom of the table view) or in the code ([tableView setTableFooterView:myButton];).
If your button should be smaller that table's width then put it inside UIView and locate as you need.
You can also add table header in a similar way...

Related

Scroll content over fixed background image in swift

I am trying to achieve a scrolling over a fixed image, as you can see on the picture.
I thought I should use a scrollview but I didn't quite get it how to use it and what to include into the scrollview, since the image and the button on the bottom should be fixed. In addition, the content should only be scrollable, when the text is to long.
I think the best solution here would be using a UITableView inside a UIViewController.
You can then set up the cells to make it look exactly the way you want.
For instance the first cell would just be the image, the second cell would be your title, the third one would be your menu bar, and so on.
This may be really useful if you plan to present dynamically the elements inside your UIViewController (for example if you need to use a database, you may want to animate the insertion of the rows only if the content has be downloaded already)
So using a UIViewController, you can just add the UITableView to it and set it up using contraints so that it fills the entire view. Then you can just add your button on the top of your table view, as a subview of your UIViewController's view (not as a subclass of your table view otherwise your button would end up scrolling too).
And again here you would need to add a few constraints to your button to make it look alright!
Just try it like this and let me know if you have any difficulties in the implementation :)
It is very complex to explain, To understand you must know good autolayout
To achieve that you need two scrollview and little math :)
Suppose your headerview height is 200
As you know when using autolayout we need following view hierarchy
Your view Hierarchy should be
--> Main View
--> ScrollView
--> Container View
-->Your HeaderView (200)
-->Content View (Equal height to UIView)
--> UIView (Your tabs like button , followbutton)
--> ScrollView 2
--> ContainerView
-->Subviews
Scroll view 1 Will used wo scroll the headerview and second scrollview will used to scroll other content of scrollview (Like Tab and text content as shown in picture )
Content View (Equal height to UIView) will allow your content view to give height of 200 extra to scroll bottom and your second scrollview will scroll to top which will allow to scroll your other content too
https://media.giphy.com/media/a2A4AQeAIkAhO/giphy.gif

Autolayout a UIScrollView to fit content including subviews and grouped tables

I am trying to present information about an object grouped into sections. It may be long, so each section uses a view to visually separate the areas and it is all in a scroll view.
The first subview has a text field that does resize to fit the text, and its parent view also resizes to fit the text field.
The second subview has a grouped table to display data. I want all of the data in the table to appear and there should be no table scrolling.
The third subview has more information but its contents are an empty view for the purposes of this demo. In the real app I may add an arbitrary number of custom subviews.
What I'm finding is that the first subview resizes appropriately but I cannot find the combination of layout constraints that will make the second subview size to fit the table and move the third view down.
What do I need to do to make the scroll view fit its contents, when the sub view's contents may resize? The table view is filling the available space in the parent view, but the parent view is not expanding to fit. I note that the third view has a top space constraint to the superview, not just to the view above it. That's visible in the screenshot below
I found a solution that like most solutions, seems obvious in retrospect.
I think my problem is that the interior tableview had no fixed height and since it also a scrollview, was trying to fit whatever was assigned instead of expanding to fit the interior content. I was able to solve my problem by adding a fixed height constraint in IB and hooking it up to an outlet in my view controller. Then in the controller:
-(void) viewWillLayoutSubviews{
[self.tableView layoutIfNeeded];
self.tableViewHeightConstraint.constant = self.tableView.contentSize.height;
}
Derp. In summary the table view needed a height so I set one.
Here's a screenshot, note the fixed height constraint on the right hand side:

How to implement smooth scrolling using both a UIScrollView and an UITableView?

Is is possible to implement smooth scrolling using both a UIScrollView and placing a UITableView at somewhere starting in the middle of the screen? The UIScrollView contentSize is elongated to accomodate the additional height of the UITableView. The UITableView should be able to contain unlimited and unknown number of rows. Scrolling the entire view downwards will cause the upper non-tableview portion to first disappear and then it scrolls down just like a regular table view.
Put it in another way, I'd like to implement a table with some fixed content before the top row. Think of it like a "header area" for the tables, scrolls up and giving way to the entire table rows.
I probably know that I can implement a TableView and programatically make the first row to contain some fixed images. In this way, it will work as expected. But which is easier to implement?
You really don't want to put the table in a scroll view. When the user scrolls on the table it will not move the underlying scrollview so you won't get the effect you are looking for.
Implement viewForHeaderInSection instead and pass in the view you want to display above the first row, or set tableViewHeader.

Can a UITableView (not UITableViewCell) have variable size?

I have a UIView (created in IB) with a grouped UITableView as a subview. Below this table view is a UIButton. The XIB containing the view will be loaded by a few different viewcontrollers, and so the contents of the table view can vary between one and four cells.
Here's what I want to achieve: when the view loads, the height of the tableview (tableView.frame.size.height) should be adjusted depending on the number of cells, and the button should be placed just beneath the table view.
Can this be done? Could it somehow be done if the view is created programmatically?
Thanks in advance
Edit: Pxl's suggestion was just what I was looking for. A while later, the need arose to have more than just a button below the table view - this was accomplished by creating a separate view containing everything I needed, and implementing the tableView:viewForFooterInSection: and tableView:heightForFooterInSection: functions.
A note for those of you trying to do the same thing: the tableview has to be programmatically created if you want different heights for the footers, or footers for only some of the sections. This is because the footer height set in IB will override the one returned from the tableView:heightForFooterInSection: function.
if there are only a handful of rows, may i suggest that you create a special UITableViewCell that contains just a button?
then make that button cell the bottom row of the last group all the time. make the group so that it will be unlabeled and appear as if the button is sitting at the bottom of your tableview. this way you won't have to muck around with recalculating the tableview's frame and redrawing it.
if the tableview will scroll due to there being many rows, then you'd be calculating the height of the tableview up to a set max (at which point the tableview will need to scroll to show more rows).
once you've determined the height of the tableview you'll need to display your rows, make a frame of the appropriate size, set the tableview's frame to it, position the button just under the tableview, and then redraw the view.
the layout and positioning in this case will need to be done programmatically.
UITableview is a subclass of UIView, so you can change its frame to suit your needs just like a UIView, and UITableView will manage drawing itself to whatever frame you give it.
Just use the methods UITableViewDataSource and UITableViewDelegate provides you.
height = [self tableView:numberOfRowsInSection]*[self tableView:heightForRowAtIndexPath:] + A_CONSTANT_FOR_HEADER_AND_FOOTER_HEIGHT
I agree with pxl that adding a cell with the button in it may be the easiest way to accomplish what you want.
Whether or not you do that, the table view's sizeToFit method should resize the view to (just) fit its contents. If that doesn't work, you can use numberOfSections and rectForSection: to build a loop that determines the height of the table's contents, and reset its frame manually. (And then position the button underneath.)

A UITableView followed by several buttons- any way to size the table view to fit a variable number of rows?

In Interface Builder, you have to set the size of the tableview and position the other elements beneath that. I want the tableview to be sized to fit any number of rows and the buttons to be moved down relative to the tableview.
Is it possible to do this without building the whole thing programmatically?
You should embed the buttons within a UIView, and set that UIView as the tableFooterView of the UITableView. You can do this in IB by dragging the view into the bottom of the UITableView. This way the buttons will always be below the last row of the tableview, though I should warn you that if there are more rows than fit on-screen, the buttons won't be visible until you scroll down. If this isn't what you want, then you'll need to do something more complicated (namely, run -layoutSubviews on the UITableView, then ask it for the rect of the last section, and use that to calculate where the buttons should go).