Can a UITableView (not UITableViewCell) have variable size? - iphone

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.)

Related

Custom cell in UITableView

I am making a custom cell which has text fields and as a result the custom cell is greater than the size of the iphone screen.But i am not being able to do horizontal scroll to reach the end of the cell.
I have tried using viewController and adding table view to it as well as creating table view controller and adding custom cell to it.
when doing it through TableViewController,I am not able to horizontally scroll it,whereas i am not getting how to add Custom cell class to the tableView object placed in viewController.I know that UITableView is a subclass of UIScrollView but not able to implement it.Please help.
Thanks
as per the documentation at
A table view in the UIKit framework is limited to a single column
because it is designed for a device with a small screen. UITableView
is a subclass of UIScrollView, which allows users to scroll through
the table, although UITableView allows vertical scrolling only.
so as desired in your case you will have to take some other approach. Now using Custom cell with TextFields should not actually need you to require Horizonatl scrolling. If the text is really large you would like to consider using TextView instead of TextFields.
Hope it helps
Ideally, you should be truncating your text. But if it is important for you to display the entire text, add a scroll view as a subview (or set it as the content view) and add your labels to the subview. Each cell will then be individually horizontally scrollable.

Let tableview's last cell scroll to half way up the screen

I have a tableview with custom cells. Each cell has a UITextfield. When the last cell's textfield is clicked, the keyboard pops up and covers it and the table view won't scroll up any further. Is there a property of UITableview that can be set so that the last cell can scroll to half way up the screen?
Thanks in advance
There are a two ways to approach this (that I can think about off the top of my head):
You can use the tableFooterView property of UITableView to set an arbitrary, empty view to be about half the size of your table.
You can add empty cells to the bottom of your table
Both of these approaches will accomplish roughly the same thing, but using the tableFooterView property is probably your best bet.
see Making a UITableView scroll when text field is selected for a list of solution..
Also basing your view controller from UITableViewController (since you say its a table) will provide all this functionality automatically!

UITableView with dynamically set size

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...

Content of custom table view cells not drawing correctly

I have several parts in my app where I use custom table view cells.
Their content is created with subviews.
The problem is that on some of these cells, the content does not appear at all or does not appear correctly until after the cell was selected for the first time.
One example is a custom cell which has a custom subview which can be set after its creation. This view does not appear at all before I selected the cell and its views were redrawn. Calling -[setNeedsDisplay] in the subview's setter method does not help either.
The problems was that I was using the cells themselves to calculate their height. For some reason, the subviews (which were part of the cell used to calculate the height) weren't appearing correctly in the cells that were used for the actual displaying.
Therefore my advice: Never use a UITableViewCell to calculate its own height. This may work in principle (it doesn't crash), but might bite you later in unexptected and hard-to-debug ways.

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).