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.
Related
Recently, I see a strange thing in Xcode 11.4.
When I create a UICollectionView with Its cell, adding TapGesture doesn't work. By debugging on its view in runtime, I noticed a view covers all the cells. It seems it is ContainerView.
This view prevents users to click or tap on items.
Any help is appreciated
Are you adding your subviews and tap gesture to the cell's contentView?
In the documentation for UICollectionViewCell (https://developer.apple.com/documentation/uikit/uicollectionviewcell), it says:
To configure the appearance of your cell, add the views needed to
present the data item’s content as subviews to the view in the
contentView property. Do not directly add subviews to the cell itself.
The same applies for UITableViewCell as well.
I need to add a scrollview within a collectionViewCell. This is confusing me because there are essentially two ways of scrolling. You can either scroll from one collectionView cell to the next or within a collectionView Cell itself, to get more info from inside the cell. I have attached a picture of what my collectionView cell looks like right now. I would like for the scrollview to scroll within the gray view. How should I set this up in my interface builder?
Thanks so much!
I have also attached the current heirarchy for my scrollview *Scrolling functionality is not working at the moment.
You have a custom collection view cell already just add a scrollView and then anything you want to display in the cell. So your hierarchy would be something like this :
Custom-Cell: ContentView > UIScrollView > then anything you want to add in the view.
I sub-classed UIcollectionViewLayout to create my own layout. In the prepareLayout() method I create the frames for the attributes of each cell because I know where they should be. The frames for each cell are hardcoded. However, I can't hard-code the position of the frames because I want to be able to drag and drop cells. UICollectionViews have nice functions for that:
`collectionView.updateInteractiveMovementTargetPosition(_:position)`.
But this function can't change the location of the cells if their position is hard-coded.
Additionally if the cell is not in it's position it seems to disappear from the view. I want to dynamically change the position of a cell when I begin to drag it. Should I call invalidateLayout() somewhere? I have a UICollectionView as a subview of my UIViewcontroller.
So I've attached a longPress gesture recognizer to my collection view.
I faced this issue few months ago, you cant reload selected cells, the only way is reload whole Collection View. If you have any other solution, pls share me. Thanks
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.
I have a UITableViewCell. How should I add a subview to it? I have seen many example to add subview to the contentView, but why not directly to the cell?
You can do it, but If you want to customize cells by adding custom view, you should add them to the contentView in order to get them animated along the table animations
When you add a custom view to contentView or assigning to contentView handles framing resizing part ,animation part and when editing mode is ON (tableView.editing = YES;) tableview tries to operate with views of itself meaning contentView, accessoryView. So when you want to add something to the table view and let control be given to tableView go for contentView