UITableView Dynamic Height - ios

I have got a UITableView in my ViewController and it's row has custom content. Every cell's height dynamically calculated according to cell's content with - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath method.
But how can set UITableView's height?
Because if my table contains only one row, after row i see a big whitespace in my table?
Isn't there any property to set table's height dynamically increase or reduce according to its contents?

The contentSize will be adjusted as needed by the number of rows and sections you have, if you want to change the actual size of the table view you should adjust the frame property
you might just be better off putting a backgroundView or colour on the table if its that the white background when you only have a few rows is bothering you

Try the contentSize method, which is inherited from UITableView’s superclass, UIScrollView. However, you may find that contentSize returns an incorrect or out of date value, so you should probably call layoutIfNeeded first to recalculate the table’s layout.
[tableView layoutIfNeeded];
// Allows you to perform layout before the drawing cycle happens. //-layoutIfNeeded forces layout early. So it will correctly return the size. Like dreaming before doing.
CGSize tableSize=tableView.contentSize;

Related

iOS: UITableViewCell - Cells interrupting/overlaying eachother

I've build a UITableView with different costum cells. One is with a UIImageView and one without. But they are overlaying each other. I've set all constraints.
every cell owns its own section.
screens:
(image and text are samples.)
Does the actual cell height change?
If so, you need to make sure you're returning the correct height with:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return heightOfCell;
}
It looks like you might have the constraints wrong on the cell without the image as it is drawing beyond where the table thinks the height suggests it should end. Hence why it is drawing beyond where the second cell top starts.
Possible issues are:
0) You are returning a height from heightForRowAtIndexPath which is too small. Use auto layout for iOS8 or if suppoting iOS7, you will need to calculate the height.
1) Perhaps your constraints are wrong. Have you pinned the bottom text to the bottom of the cell contentView and the top of the date to the top of the contentView?
2) If using iOS8 auto layout, try adding [self.tableView reloadData]; in viewWillAppear. Sometimes it needs an extra kick to get the layout right, especially if text needs resized. I have no explanation for this other than I had to do this and I've seen it mentioned a few times elsewhere.

UITableViewCell's rectForSection returns estimate when section not visible

I'm currently trying to add infinite scrolling to a UITableView, which contains a number of calendar events. Since the event title doesn't always fit inside a single line I've added a multi-line UILabel to the cell. In order to calculate the height of the cell I'm taking advantage of UITableView's new self-sizing cells via Auto Layout in iOS 8. WWDC Session 226 talks about this in more detail.
In order to implement the infinite scrolling mechanism I'm overriding layoutSubviews where I need to calculate the height of a section which at the time given ist not visible on screen. This can be done by using [self rectForSection:0]. When doing so the table returns a height based on the estimated row size I had to define inside the table's initialiser in order to make self-sizing cells work.
self.estimatedRowHeight = 44.0;
self.rowHeight = UITableViewAutomaticDimension;
When the section gets on screen I get the correct size of the section, but since I have to update the table's contentOffset based on the computed height of that specific section this causes my table to jump up and down.
Any ideas on how to solve this? Is there a way to force a section to return the actual height and not the estimated one?

Change UITableViewCell Height after creation

I'm working on an app which uses tableviews which displays content fetched from a webservice. In some cells I may need to display an image with unknown dimensions. To do this, I have an UIImageView inside the cell of a fixed width (292pts). I then adjust the imageview height accordingly, like this.
imageviewheight = 292/imagewidth*imageheight
For example, if I have an image that is 730*1250 I set the height to 500 to maintain the aspect ratio and show the full image.
However, this can be a problem when I need to set the cell height. Since the images are downloaded from the internet, the image may not have finished downloading when I need to create the cell. Thus, I cannot determine the cell height until the images have downloaded, and I can't have my UI wait for that, so I display a placeholder image instead, and the cell height is based off the height of that.
Once my image has downloaded I need to display it in the image view. That would be fine, if the height matched the height of the placeholder image. Otherwise, I need to adjust the imageview height to compensate, and in turn change the cell height as well. But, the problem is I can't change the height once the cell has been created.
The only solution that I can think of is saving the UIImage, and reloading the cell. Then, since I already have the image, I can setup the cell height accordingly based off the image. However this seems like a very inefficient solution. Is there any way to adjust the cell height after it has been created? I tried changing the cell's frame, but that didn't do anything. Is there a way to change the cell height after creation, or is reloading the only way?
Once the image is loaded, be sure you're returning the correct new height for the row from your UITableViewDelegate in the method - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath. You may need to call - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation to force it to re-check the value for the height of that row.

Wrong height of UITableViewCell

I have a strange issue with the height of the table view cells. I'm creating a custom grouped style UITableView in the storyboard. I have 4 different prototype cells for a single, top, middle and a bottom cell. Each of them has a different height. I'm already setting those heights in the storyboard, and I've also implemented the delegate method
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
Just for an example: The single cell should have a height of 48. So I set the height in the storyboard to 48 and the above mentioned delegate method returns the value 48. But if I take a look at the screenshot, the single cell has actually a height of 49.
There is the same behavior for the top cell, but not for the middle and the bottom cell. That makes it even stranger.
Do you have any ideas? Where comes this extra pixel from?
I don't know exactly but it may be reason check the height of the header and footer .

UITableView scrollable content size

I am trying to achieve a newsstand like effect with a scrolling and repeating background. I could do this using a UITableView if I could set it's content size (An inherited method that seems to be overridden by something else in the UITableView Code) in order to fill the view with unscrollable cells.
Currently I am planning to make a custom uiscrollview. Which will be more complex and won't have the cell reuse.
So, is there a way to set a UITableView's content size.
You could semi-hack a 'contentSize' kind of control over a UITableView by simply setting the number of cells that you have (let's assume you have a 1 section table view and aren't using a 'grouped' styled table) to:
your desired height contentSize height / height for each cell (default is 44)
Now, your problem is that if you only have, say, three rows' worth of data to display, what to do with the rest of the cells? Well, simply set their backgroundColor to the same as the table's background color and their selectionStyle property to UITableViewCellSelectionStyleNone. Wham, now you have invisible cells, and can set the 'contentSize' of the table.
Note that your contentSize will only have a resolution of the height of your cells, if you leave the cell heights at their default 44, then you can only set the contentSize to a multiple of 44.

Resources