How to preLoad UICollectionVewCell? - ios

I have all the UICollectionView dataSource, but the subViews in cell is not all the same.So need remove the old subViews and add the subViews when the cell showing again. Want to know any way to preload UICollectionViewCell or Make it more smoothly.

Instead of "preloading" the cells, I would suggest subclassing UITableViewCell for each of your different types of cells, and adding the subviews in their awakeFromNib method. I would recommend against adding subviews in your tableview:cellForRowAtIndexPath: method, you should just be updating the content (e.g. UILabel values).
If you add subviews inside tableView:cellForRowAtIndexPath" method, you will need to remove all of the subviews first. Otherwise if they are "reused" the subviews will be added on top of the existing ones.

Related

iOS: UICollectionViewCell ContentView subview always count One

How to add dynamic view inside UICollectionViewCell but when I scroll UICollectionView the view has remove and add last index of cell.
Now I have add view on all Appear cell.
You should be adding/removing subviews at cellForItemAtIndexPath().
That is to say, you should probably removeAllSubviews() and then cell.addSubview(yourSubview) every time.
Try to add this
cell.contentView.removeAllSubviews()
cell.contentView.addSubview(yourSubview)
May this help u
let me know if it works.

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

How to add a subview to a UIView's contentView in a UIStoryboard? (iOS)

I have a UITableView that I've created in a UIStoryboard. It contains one Prototype UITableViewCell. The cells are populated in the ViewController's cellForRowAtIndexPath: method.
I'm now trying to add the ability to delete cells using commitEditingStyle:forRowAtIndexPath:. Everything is working except that when the delete button animates over the cell, the rest of the content doesn't shift over and it looks really ugly. According to this SO question, content will only be shifted if it's added to the cell's contentView rather than to the cell itself.
As such, is there a way to add a UIView (Ex. a UIButton) to a UITableViewCell's contentView within a UIStoryboard?

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.

Subviews of UITableViewCell are not visible while reordering

When I reorder my UITableViewCells, the Subviews of the Cell are not visible while dragging...I get always the same result, whether I add the Subviews programmatically in a UITableViewCell Subclass or in Storyboard...
Is there a possibility to see the real UITableViewCell with Subviews while dragging?
Ok I found a solution by my self...
For some reason the mysterious reordering animation from Apple cant capture simple UIViews, but with UIImageViews, UIButtons and UILabels as Subviews of the custom TableViewCell, the animation works like expected...creepy!

Resources