Drawing really long text on iPhone - iphone

Currently I have UITableViewCell's that hold sometimes really long text (up to 50,000 pixels in height after drawing). However the UITableView delegate documentation says that cells shouldn't be higher than 2009 pixels in height (any ideas why?).
It's only the first section in my table view that has the really long cell, so instead of using a cell for the first section, I thought I'd create a UIScrollView, put a UITextView as the first "cell" and add it to the scrollView, and then add the tableView to the scroll view as well (under the textView). However, having a 50,000 px high UITextView is causing huge memory problems.
What are my options? I know I could use a UITextView that scrolls, but to have a scrollable UITextView with a tableView just causes complicated scrolling behavior. I want to mimic the scrolling of a tableView.
I didn't know it would be an issue to have a 50,000 px high view in a UIScrollView. I thought that's what UIScrollView's are for? Do I have any alternatives?

I would seriously question the UI design where you must render text that large as part of a table cell. The best option would be to put a reasonably-sized summary in a cell with cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;, build a separate view for the long text, and let the user navigate to that view by clicking the disclosure indicator.
As a side note, you could also put a scroll view inside the initial table cell (not all cells must be of the same type; you can make one with a scroll view in it, and use it for the cell at index zero). It's not going to be as usable as the regular cell with a disclosure indicator, though.

Related

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.

iPhone: How to Place a Scrollview Inside of Tableview Cell

I want to put a scroll view inside of the table view cell. I have a number of buttons in one cell of table view and I need to scroll that cell to show the appropriate button.
If you want to use a vertical scroll view then I wouldn't suggest you doing it because, as TechZen wrote, there will be a mess in this case.
If you want the inner scroll view to scroll horizontally then it might be achieved by 2 ways:
Implement a custom table view cell that will include a scroll view inside it.
Add a scroll view as a sub-view to your cell that you will return from tableView:cellForRowAtIndexPath: method.
I suggest you to use the second approach.
There are plenty of examples online. Usually the sub-views are labels or image views, but it is not complicated at all to add a scroll view instead...
I don't think you can do this. It would require nesting of scrollviews which isn't really supported.
Even if it was, it would be very difficult for a user to know which scrollview they were hitting with their pudgy finger. Remember, you don't have the one pixel precision of a mouse on the iPhone. You have an area of at least 15x15 pixels. You don't have a scroll bar but instead just drags anywhere on the screen.
Instead, you should use a master-detail pattern. Selecting the cell in the tableview pushes a detail view which has the scroll view with all the buttons.
Why do you want to do it like this?
I think the best idea is to draw your table view manually above your uiscrollview. I did it, and it works. It just takes more effort and drawing accuracy. But that takes a lot of time. :)

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

UITableViewCell's contentView's width with a given accessory type

On an iPhone, how do you figure out the width of a table view cell's content view when it is showing a certain accessory view (disclosure indicator, etc)?
I need this in order to calculate the correct cell height for cells that contain wrapping, variable-length text. But when the table view delegate is asked for a cell height, it doesn't actually have the actual cell instance, so it can't just query the content view bounds directly.
I can easily hard-code a 20-pixel margin on the right which appears to be accurate for a plain style table view in portrait orientation with a disclosure indicator, but would prefer to do it the Right Way so that it keeps working if Apple decides to tweak the margin.
(This is related to this question.)
Personally, I would just hard code the values -- simpler and things will break in a predictable way.
But were I to do this programmatically, I would create a UITableViewCell, set up the editing properties / accessory views you need to measure, and then ask it how big its contentView is.
Of course I would probably heavily cache this -- doing allocations when asking UITableView asks you for height information sounds to me like it would be slow (check with a profiler first though, as always).
I would have a subclass of UITableViewCell that holds all its subelements. You can cange the frame of certain elements when the cell enters and exists editing mode. There is a good example of this in Apple's Table View Programming guide under the section on creating a custom table view cell.
I believe the UITableViewCell's contentView property is the view that contains your labels etc., so the width of that should be your available size to use.