UITableView Cells Expand With A Delay - swift

I am making a to do list app and have 3 custom cell types. For task cells, it is just an imageview on the left and a textview on the right. I would like the cell to automatically resize itself when the textview text is to large for a single line.
I have added the correct constraints and have added this to viewWillAppear
tableView.estimatedRowHeight = 30
tableView.rowHeight = UITableView.automaticDimension
However, when going into the view with the tableview, the cells remain small for a second and then expand to their correct size. Also, when initially adding the task, the cell doesn't resize unless either going back and opening the view controller again or the tableview is scrolled up until the cell is out of view and then letting go.
This is very odd behaviour and even happens when removing the two lines above from viewWillAppear. It just happens for no reason.
I would like the cells to be of the right size when going into the view and not resizing a second later.
An example of what this looks like is below

Okay so I finally found the answer. I made my cell programmatically with auto layout and some of that was setup in the layoutSubviews() method inside the cell. This method is only called when needed. So when scrolling up or down.
To fix this, in your cellForRowAt method, just call
cell.layoutSubviews()

Related

UITableViewCell StackView Hidden Elements

I have a TableViewCell that has 2 StackViews. One of the StackViews has 3 components and one of which is hidden. By pressing on a button, I want the hidden UIImageView to appear. While my code does this, it does not format it correctly as the height of the TableViewCell does not change, as I would like it to. I have tried calls to sizeToFit(), but I am starting to realize that this will not affect the height of a cell. When the cell leaves the view and comes back, it draws correctly. How can I update the height of the cell when the button is pressed?
After writing out this question, I started to realize that the problem was from the cell's height. After doing a little bit more research I found out that this can be achieved with the
tableView.beginUpdates()
tableView.endUpdates()
I had to create a delegate and add a delegate method to reference back to the ViewController.
Hope this helps someone else!

UITableViewCell subview not displaying until redraw

This problem is driving me nuts.
I have a prototype UITableViewCell in my storyboard. It contains a bunch of subviews. One of the subviews, a UILabel, is misbehaving.
When the tableview loads and is displayed for the first time, all of the cells look fine. However, as the tableview scrolls down, eventually one of the cells shows up without the UILabel subview. It seems that it is always the first cell that is being recycled.
If I continue scrolling down the tableview, however, as soon as the misbehaving cell is clipped by the top of the tableview (when it begins to be scrolled off the top of the screen), the label appears just as it should.
So it seems that the label is there, and it receives the string that I assign to it in my cellForRowAtIndexPath method. It's just not getting drawn for some reason. I've tried inserting setNeedsDisplay messages in various places, but that hasn't helped.
What is also strange is that I'm using the very same UITableViewCell subclass with a duplicate view hierarchy in the storyboard in a different view controller, and there I don't have any trouble.
Anybody have some idea of how I can start to unravel this mystery?
Thanks!
It sounds like it's only a problem when you use a reusable cell, which means that the values on reusable cells may not be getting set until you're ready to scroll off.

iphone tableview scrolling issue

I have a tableview with imageviews and textviews in each cell. When my view is loaded one of the textviews is invisible. But when I start scrolling the textview appears, so that my view looks as it is supposed to look. Does anyone know why this is happening?
P.S. I have read about reusing cells when scrolling so I have been very careful to construct my cells correctly.
Without code, no one can give you a exact answer. But for a guess....
Is your data is getting populated after your table call cellForRowAtIndexPath? When the cell populates the first time, no data, so the cell is in it's unformatted state. By the time you can interact with the table and scroll it off and on screen (which calls cellForRowAtIndexPath again), the data has been populated and so the cell looks as expected.
You can test this by putting a breakpoint in your cellForRowAtIndexPath method and check to see if your data objects are initialized or still set to nil.

UITableView separator at wrong position

On selection I change the height of an UITableViewCell (loaded from a nib).
But the separator line is at a wrong position when I do this.
In the screenshot the first row is selected, and therefore bigger than the other ones.
From the separator positions it looks like the cell after the selected cell would be the big one. The second cell "has" exactly the size the first cell should have.
To change the height I save the selected indexpath in tableView:didSelectRowAtIndexPath: and compare it in tableView:heightForRowAtIndexPath:. If the indexpaths are the same I return the increased height. With the help of some NSLog I made sure that the correct height is returned.
And if I would resize the wrong cells the views of the cell would overlap, this doesn't happen.
If I click Line 3 of the first cell the tableView:didSelectRowAtIndexPath: fires and the indexpath is the one for the first cell. So I guess the heights are correct, and the tableview draws the separators on the wrong position.
Does anybody has an idea what I did wrong?
Any solutions? Or should I file another bug with apple?
Edit: If I don't reuse my cells it works as expected.
It turned out that I had switched off "Autoresize subviews" for the UITableViewCell.
If I turn that option on it works as expected.
I had a similar problem. In my case I was using layoutSubviews to do custom layout on my cell. The separator was in the wrong position and the layout of the accessory view was also out.
In my case the issue was failing to call [super layoutSubviews].

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