UITableViewCell StackView Hidden Elements - swift

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!

Related

UITableView Cells Expand With A Delay

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

Expanding a UIView in a UITableViewCell as well as the cell itself on click

I have a TableViewCell with three Views. I'm using AutoLayout and UITableViewAutomaticDimension for the rowHeight. What I want to achieve is that when a user clicks on a cell, the blue view as well as the whole TableViewCell gets expanded (animated), like this ...
I couldn't come up with an idea to solve this, does anyone has a hint?

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.

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

removeFromSuperview only removes view from last cell

I have a UITableView set up with a custom delete button which consists of a UIButton (btnDel) added as a subview to the UITableView's cells. The delete button is added as a subview when my edit button is pressed. This works well but when I try to remove the subview using:
[btnDel removeFromSuperview];
It only removes the button from the last cell and the other cells still retain the now removed button. I've tried this in many different ways and still can't figure it out. I've tried using functions such as turning the opacity of the button to 0 or setting Hidden to YES, but like the removeFromSuperview, it only effects the button in the last cell with the others staying the same.
Any help is greatly appreciated and if anymore of my code is needed let me know.
If you don't keep references you can't tell which button belongs to which cell.
You might want to subclass UITableViewCell so that your cells have a property which points to the button (assign the button to the property right after instantiating the button). You could then use this property to access (enable, disable...) the buttons later on.