UITableViewCell doesn't resize when UIStackView changes its arranged subviews - uitableview

I'm using Auto Layout for my UITableView and it's custom cells. One of these cells has a UIStackView to which I add and remove arranged subviews, however, when its arranged subviews changes the cell itself does not resize.
Calling the reloadData() method is a fix for this problem, however, I only want to update the one cell that changes.
When I try calling reloadRowsAtIndexPaths(_:withRowAnimation:) however, it only updates to show the previous change on the arranged subviews. So for example, if I add one arranged subview, say a red one, then nothing appears to happen (even though the subviews are added when I check), then adding a second causes the cell to reload but only update to show the previous red arranged subview that was added and so forth.
Is there any way of forcing specific cells to layout in the UITableView?
Update: Here is a link to an example project showing the issue:

It seems the answer posted here works. I've opted for the non-animated version of the answer but both seem to work.

Related

Adding subview to UICollectionView - disables scrolling

I want to add a subview to a UICollectionView in order to make a left panel that scrolls with the collectionview.
Using
[self.collectionView addSubview:myView]
all touches become disabled and I can no longer scroll the view. I've read that adding a subview to a collectionView like this is bad practice.. is this true? Why does it disable touches from reaching the collectionView event when
userInteractionEnabled = NO
I'm trying to do this: imgur link by grabbing the frame position of the first cell in each section, and adding a dot with to myView with the same y value.
Thanks for any help!
Adding subviews using the addSubview: method to a UICollectionView is very bad practice. It can cause a lot of different problems in the normal behaviour of the CollectionView. It can obstruct the views underneath it, capture the touch events, preventing them from reaching the actual scrollView inside the CollectionView, etc. The subviews added using this method will also not scroll as the other elements in the CollectionView do.
The correct way to do what you want is to implement a new type of UICollectionViewCell for the dots, and compute their locations in the prepareForLayout and layoutAttributesForElementsInRect: methods. Basically you'll have either one or two cells in each row. Which ones will have two rows will be determined by you in the methods I've mentioned.
In fact, Apple's docs have a perfect example that's even more complex than what you're trying you achieve. You should check it out from this link.
May I know the purpose of that scroll view ? Because, if you're looking for a subview that displays only a label or image etc., You can use custom collectionview cell instead if I am not wrong... Hope it helps :) :)

UICollectionView within UITableView. How to tell tableview cell to change height when collection view gets taller?

I have a UITableView and one of my table cells is a UICollectionViewController subclass that contains a UICollectionView of displayed email addresses. When a user adds an email the UICollectionView and it’s cell in the table view should get taller.
I’m currently attempting to do this setting my collectionView height constraint to collectionView.contentSize.height in the LayoutSubviews method of my collection controller/cell class. My issue is that the cell in the UITableView is not changing size when this happens.
I am assuming that this is because there is nothing telling the table view that the height of the email entry cell has changed. I am currently using dynamic cell sizing - or trying to anyway. Even, if I call tableView.reloadData() this still does not work. I’m wondering if someone could give me a high-level idea of how they would set this up.
UPDATE:
I had a broken constraint issue that was part of my problem, but this is still not solved. While have proven that I can update the height constraint and that will update the size of the collection view, it's out of sync. It's always one update behind. Here's an image showing the 'UICollectionView' in green and you can see in the logs that I'm updating the constraint (yes, multiple times) each time after adding a new item to the collection, but it is not updating the bounds of the view instance. In this example, if I were to add a new item to the collection, the next time I inspect bounds.height it would be at 149.5. What am I missing!?
i had answered this question in another tableview inside tableview cell that will work for collection view too ,i explained it in this thread
ask me if you faced any problem
It seems you want to set the height of your table view cell dynamically. This detailed tutorial walks you through making a custom cell class that will have dynamic height. Unfortunately, this tutorial only walks you through how to do this with auto layout constraints and a storyboard. In the tutorial you'll notice that the height of the cell depends on the constraints put on a title label within the cell. Try doing the same thing, but applying the constraints on the content in your cell (the collection view cell).
http://www.raywenderlich.com/73602/dynamic-table-view-cell-height-auto-layout

Removing a static UIView from a UITableView with Prototype Cells

I have a storyboard I'm maintaining that is a UITableViewController.
The UITableView is set to use Dynamic Prototypes and has 2 cells.
Above those two cells is a UIView inserted within the table view. That view contains a UIButton and another custom UIView. Both have constraints. The effect is that we have a table view with a button above it.
Now, in certain conditions, we want to remove that button and have the rest of the table view move up in the UI. I can remove that button -- by itself or by removing that parent UIView, but I cannot seem to to get the table to change the position of the prototype cells.
To hopefully make it clear, my document outline looks like this:
Table View
View (buttonContainerView mentioned below)
UIButton
View (child view mentioned below)
Constraints
Prototype Cell 1
Prototype Cell 2
I have tried several different approaches to remove the button. After reading that removing it from the superview will remove constraints as well, that's the approach I currently have in place.
[self.buttonContainerView removeFromSuperview];
I've tried removing the button, the child view, and the container view. I've also tried explicitly removing the constraints then removing the views. None of these seemed to work.
I've also tried the above with calls to setNeedsLayout and setNeedsUpdateConstraints on the table view, as well as setNeedsDisplay on the view.
I've also tried setting the row height to 0 for that first view, but it turns out it's not a UITableViewCell, so that doesn't work, either.
Of course, if I delete that container view from the storyboard, the prototype cells do appear in the UI exactly where they should.
Can you sense the flailing about?
What else could I be missing here to remove that top view effectively?
What I didn't realize is that the setup I have is a UITableView with a table header. To remove it and fix the spacing problem I was having I just needed to set self.tableView.tableHeaderView = nil in viewWillAppear:.

Removing subview from cell for auto layout issue

What is the best way to remove auto layout constraints for a Cell when a view should not be shown?
We have a cell which has a layout with around 6-7 views. One of those views is for a star rating. When the star rating is not available we do not want to show the view. At present we hide the view but this leaves the auto layout constraints in place.
Similar question - How to use auto-layout to move other views when a view is hidden?
This is the view in question hilighted above. We would ideally like to remove this view from its superview when there is no available star rating. The issue we have is that if we remove the view from superview removeFromSuperview in cellForRow... then the next cell would be affected, because the view is not added again.
(I would comment to request clarification, but do not yet have the reputation.)
Is there a chance you could just hide the view for the cell in question?
If I understand what you are saying, you are suggesting that removing the view in question from it's superview creates an issue when you are creating a new cell. So, when you dequeue a new cell, just check your star-count property, and if it is >0 for this next cell, then show the view for that cell.
Another option is to pin the surrounding views to their parent view rather than pinning them to this view that you want to remove. This way, when you remove the star-rating view, the layout constraints for the surrounding views remain unchanged.
remember the constraint (for show/hide a view) in to variable and remove it constrains.
and delete this constrains and replace with new constrains if you need change view.
For example:
view has width and height for show
and replace constraint where width and height will be zero for hide view.
The best route I found was to create separate cell layouts and decide which layout to use based on if the information was available. This meant creating a second prototype cell in IB without the view in question and different constraints but works as expected. Open to other suggestions on this one.

UITableView Visible Cells Not Laying Out Subviews When View Appears

I have a UITableView that has multiple sections, with custom UITableViewCell subclasses populating the table. Within these UITableViewCell subclasses, I am implementing layoutSubviews to customize the position of labels, etc.
All of the cell's subviews are adjusting as they should, except the first batch of visible cells upon the view loading. They are identical to how they are laid out in storyboard, which is not what I want. For example:
http://i.stack.imgur.com/L5GJh.png
Note: The green and orange borders are a visual aid to see if the labels are resizing.
Upon scrolling, all of the new cells that appear have their subviews the way that I programmed them to be in layoutSubviews. As far as the first batch of visible cells, I can scroll them offscreen and then back on, and then the subviews are laid out perfectly. For example:
http://i.stack.imgur.com/jgjch.png
Within tableView: cellForRowAtIndexPath:, I call [cell layoutIfNeeded] before the method returns the cell. If I change this to [cell layoutSubviews], then the inverse happens, where the first batch of visible cells are laid out as they should be, but all of the cells that get loaded upon scrolling are not laid out properly.
I have tried to put [cell layoutIfNeeded] within [tableview: willDisplayCell: with no luck. Any ideas on how to fix this?
Thanks in advance!
This is the behavior you'd see if you were setting frames in your layout code with Auto Layout enabled. This won't work. The Auto Layout system is responsible for setting frames and will overwrite the values you set.
You should either specify your layout using constraints or turn off Auto Layout.

Resources