Dynamic height noScrolable UICollectionView in UICollectionViewCell - ios

I want to add a dynamic height nonScrollable UICollectionView into UICollectionViewCell. (It is easy to add a static height UICollectionView in UICollectionViewCell as explained by Ash Furrow)
After spending hours on searching through the web I still don't get it.
This is what I want
Lets call:
Main CollectionView: ParentCollectionView
Inside CollectionView: ChildCollectionView
I want to get the height of ChildCollectionView so that i can set the cell size of the ParentCollectionView in side:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let text = "I'm trying to get a label in a cell.."
let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)
let estimatedFrameForEventText = (text as NSString).boundingRect(with: CGSize.init(width: view.frame.width - 30, height: 1000), options: options, attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 12)], context: nil)
return CGSize.init(width: view.frame.width - 10, height: 320 + estimatedFrameForEventText.height + 60)
}
Also want to change the height constant of the ChildCollectionView with this:
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: eventCellId, for: indexPath) as! EventCell
cell.eventText.text = "I'm trying to get a label in a cell.."
// height constraint of ChildCollectionView in ParentCell
cell.collectionViewHeightConstraint.constant = cell.collectionView.intrinsicContentSize.height
return cell
}
The scrolling of ChildCollectionView is purposely disabled to show the ChildCollectionViewCells all together.
This is where I am stuck
I am not abel to preCalculate the ChildCollectionView to pass them in the ParentCollectionView delegates
Both the CollectionView uses UICollectionViewFlowLayout
I am new to iOS developing.
Any help would be appreciated :)

This solves my problem
ParentCollectionViewController
var height: CGFloat!
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: eventCellId, for: indexPath) as! EventCell
cell.collectionViewHeightConstraint.constant = height!
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)
var sizeOfChildCell: CGRect!
var widthOfCells: CGFloat! = 0.0
let cellPadding = 10
let widthOfChildCollectionview = view.frame.width - 120
for tag in tags {
sizeOfChildCell = ("#\(tag)" as NSString).boundingRect(with: CGSize.init(width: widthOfChildCollectionview, height: 1000), options: options, attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 12)], context: nil)
widthOfCells = widthOfCells + sizeOfChildCell.width + cellPadding + 3.8
}
let numOfRows = ceil(widthOfCells / widthOfChildCollectionview)
height = numOfRows * 25
height = height + numOfRows * 3.8
return CGSize.init(width: view.frame.width - 10, height: 320 + height)
}
ParentCollectionViewCell
var childCollectionViewHeightConstraint: NSLayoutConstraint!
lazy var childCollectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.showsVerticalScrollIndicator = false
cv.isScrollEnabled = false
cv.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)
cv.backgroundColor = .white
cv.dataSource = self
cv.delegate = self
return cv
}()
override init(frame: CGRect) {
super.init(frame: frame)
childCollectionViewHeightConstraint = NSLayoutConstraint(item: childCollectionView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 96)
addConstraint(childCollectionViewHeightConstraint)
}

Just for future searches on this subject. I'm using IGListKit with a cell that contains a UICollectionView that holds my tags. My problem was that IGListKit isn't built very well on the AutoLayout principles so to prevent the collectionview cells from overlapping each other I had to calculate the height for the section manually before it's shown.
My idea was to just add the width of each tag (+ padding + minimumInterimSpace) and determine when the next tag supersedes the collection view width.
func calculateDimensions() {
//This is how IGListKit forwards the width, could be replaced with collectionView.containerSize.width
let width = collectionContext!.containerSize.width
var lineWidth:CGFloat = 0.0
var lines = 0
for tag in data.arrTags {
//Calculate bounds by using boundingRect
let size = TextSize.size(tag, font: Font.fontRegular(size: 13), height: 30)
//16= 8 padding-left + 8 padding-right
//5 = minimumInterimSpace
let itemWidth = 16 + size.width + 5
let tmpWidth = lineWidth + itemWidth
if tmpWidth > width {
lines += 1
lineWidth = itemWidth
}
lineWidth += itemWidth
}
sectionHeight = CGFloat( lines * 30 + (lines * 5)) + 15
}
This method is called when IGListKit sets the size for the current section. So I did this:
override func sizeForItem(at index: Int) -> CGSize {
calculateDimensions()
let size = CGSize(width: collectionContext!.containerSize.width, height: sectionHeight)
return size
}
This will result in the following:

Related

UIImage not being added to all cells in UICollectionView

I have added a collection view and added an image view for each cell. I was able to add and scale the image views for each cell, but they were slightly off centre. I set the image view's centre to the cell's centre, but this caused only one image to now show.
Here is the code:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CarCell", for: indexPath)
let carImageView = UIImageView(image: UIImage(systemName: "car"))
carImageView.frame = cell.contentView.frame
carImageView.center = cell.center
carImageView.contentMode = .scaleAspectFit
cell.addSubview(carImageView)
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let columns: CGFloat = 2
let collectionViewWidth = collectionView.bounds.width
let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout
let spaceBetweenCells = flowLayout.minimumInteritemSpacing * (columns - 1)
let adjustedWidth = collectionViewWidth - spaceBetweenCells
let width: CGFloat = floor(adjustedWidth / columns)
let height: CGFloat = width
return CGSize(width: width, height: height)
}
Here is the outcome:
And the view hierarchy shows all cells are created with their UIViews, but not the image views:
Removing the line carImageView.center = cell.center makes all images reappear again, but off-centre. Does anyone know how to fix this?
Can you try with the following?
let carImageView = UIImageView(image: UIImage(systemName: "car"))
carImageView.frame = cell.contentView.bounds
carImageView.center = CGPoint(x: cell.bounds.width/2, y: cell.bounds.height/2)
carImageView.contentMode = .scaleAspectFit
cell.addSubview(carImageView)

How to show two columns in a CollectionView using swift4 in all devices

To show only two columns in a collectionView i am using this piece of code
let layout = self.collectionView.collectionViewLayout as! UICollectionViewFlowLayout
layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0)
layout.minimumInteritemSpacing = 4
layout.itemSize = CGSize(width:(self.collectionView.frame.size.width - 10)/2,height: (self.collectionView.frame.size.height)/3)
but it is only showing properly on 5s, not on every iphone. please help.
i want to show my view controller like this image. please help anyone
You must be missing UICollectionViewDelegateFlowLayout
Try this and see:
// Source code
import UIKit
class CollectionVC: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
#IBOutlet var collection: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
collection.delegate = self
collection.dataSource = self
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
//collection.reloadData()
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let flowayout = collectionViewLayout as? UICollectionViewFlowLayout
let space: CGFloat = (flowayout?.minimumInteritemSpacing ?? 0.0) + (flowayout?.sectionInset.left ?? 0.0) + (flowayout?.sectionInset.right ?? 0.0)
let size:CGFloat = (collection.frame.size.width - space) / 2.0
return CGSize(width: size, height: size)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 50
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "testcell", for: indexPath)
cell.backgroundColor = UIColor.red
return cell
}
}
// Interface Design
Result:
Make sure your protocol confirms to your ViewController,
class ViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout
And set flow layout to your collectionview object
In viewDidLoad
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical //.horizontal
layout.minimumLineSpacing = 5
layout.minimumInteritemSpacing = 5
collectionView.setCollectionViewLayout(layout, animated: true)
And implement following methods
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 1.0, left: 1.0, bottom: 1.0, right: 1.0)//here your custom value for spacing
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let lay = collectionViewLayout as! UICollectionViewFlowLayout
let widthPerItem = collectionView.frame.width / 2 - lay.minimumInteritemSpacing
return CGSize(width:widthPerItem, height:100)
}
add this code to viewDidLoad.
change height according to you.
try this!
if let layout = collectionView?.collectionViewLayout as? UICollectionViewFlowLayout{
layout.minimumLineSpacing = 10
layout.minimumInteritemSpacing = 10
layout.sectionInset = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)
let size = CGSize(width:(collectionView!.bounds.width-30)/2, height: 250)
layout.itemSize = size
}
Update for Swift 5 and XCode 12.4
import UIKit
class TwoColumnViewController: UIViewController {
enum Section {
case main
}
var dataSource: UICollectionViewDiffableDataSource<Section, Int>! = nil
var collectionView: UICollectionView! = nil
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Two-Column Grid"
configureHierarchy()
configureDataSource()
}
}
extension TwoColumnViewController {
/// - Tag: TwoColumn
func createLayout() -> UICollectionViewLayout {
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
heightDimension: .fractionalHeight(1.0))
let item = NSCollectionLayoutItem(layoutSize: itemSize)
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
heightDimension: .absolute(44))
let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitem: item, count: 2)
let spacing = CGFloat(10)
group.interItemSpacing = .fixed(spacing)
let section = NSCollectionLayoutSection(group: group)
section.interGroupSpacing = spacing
section.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 10, bottom: 0, trailing: 10)
let layout = UICollectionViewCompositionalLayout(section: section)
return layout
}
}
extension TwoColumnViewController {
func configureHierarchy() {
collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: createLayout())
collectionView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
collectionView.backgroundColor = .systemBackground
view.addSubview(collectionView)
}
func configureDataSource() {
let cellRegistration = UICollectionView.CellRegistration<TextCell, Int> { (cell, indexPath, identifier) in
// Populate the cell with our item description.
cell.label.text = "\(identifier)"
cell.contentView.backgroundColor = .cornflowerBlue
cell.layer.borderColor = UIColor.black.cgColor
cell.layer.borderWidth = 1
cell.label.textAlignment = .center
cell.label.font = UIFont.preferredFont(forTextStyle: .title1)
}
dataSource = UICollectionViewDiffableDataSource<Section, Int>(collectionView: collectionView) {
(collectionView: UICollectionView, indexPath: IndexPath, identifier: Int) -> UICollectionViewCell? in
// Return the cell.
return collectionView.dequeueConfiguredReusableCell(using: cellRegistration, for: indexPath, item: identifier)
}
// initial data
var snapshot = NSDiffableDataSourceSnapshot<Section, Int>()
snapshot.appendSections([.main])
snapshot.appendItems(Array(0..<94))
dataSource.apply(snapshot, animatingDifferences: false)
}
}
I have following code which I have implemented to display two column in collection view.
Add UICollectionViewDelegateFlowLayout’s following method method:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let size = (collectionView.frame.size.width - 30) / 2
return CGSize(width: size, height: size)
}
Where 30 stand for left, right and middle space between cell.
i.e Cell should be add 10 pixel on left Side, 10 pixel on right side and 10 pixel in between two cell.
Select you collectionview from storyboard and from Size Inspector set following minimum spacing.
I hope this will fix you issue.
Let me know if any query.

Swift CollectionViewCell Auto Height

I try to make my cell to auto resize with the content of a label.
My Cell contains two labels. The first label is always one line, the other can be multiline.
The problem is that the cell won't resize. This is my code :
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
CollectionViewHeight.constant = CGFloat(60 + 60 * commentaires.count)
return commentaires.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = commentCollectionView.dequeueReusableCell(withReuseIdentifier: "customCell", for: indexPath) as! CustomCollectionViewCell
cell.contentCommentLabel.text = commentaires[indexPath.row]
cell.nameCommentLabel.text = commentairesNom[indexPath.row]
cell.contentView.systemLayoutSizeFitting(UILayoutFittingCompressedSize)
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
// This is Just for example , for the scenario Step-I -> 1
let yourWidthOfLable = self.view.bounds.size.width
let font = UIFont(name: "System", size: 14.0)
var expectedHeight = heightForLable(text: commentaires[indexPath.row], font: font!, width:yourWidthOfLable )
print("expectedHeight : " + String(describing: expectedHeight))
return CGSize(width: view.frame.width, height: expectedHeight)
}
func heightForLable(text:String, font:UIFont, width:CGFloat) -> CGFloat{
let label:UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: width, height: CGFloat.greatestFiniteMagnitude))
label.numberOfLines = 0
label.lineBreakMode = NSLineBreakMode.byWordWrapping
label.font = font
label.text = text
label.sizeToFit()
return label.frame.height
}
You need to get height of UILabel programmatically according to font family, font size and width of UILabel and than after add this height in top UILabel's height. This is your total height of cell.
Usually i use this code to configure my CollectionView:
private func config_collection()
{
self.collection_view.delegate = self
self.collection_view.dataSource = self
self.collection_view.keyboardDismissMode = .interactive
self.collection_view.alwaysBounceVertical = true
self.collection_view.showsVerticalScrollIndicator = true
self.collection_view.showsHorizontalScrollIndicator = false
if let layout = self.collection_view.collectionViewLayout as? UICollectionViewFlowLayout
{
layout.scrollDirection = .vertical
layout.minimumLineSpacing = 2
}
}
extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
{
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width, height: 50)
}
}
Edit 1:
To resize the cell by estimate height i use this code:
func get_size_height_text(texto: String,
ui_view: UIView) -> CGFloat
{
let l : [NSAttributedStringKey : Any] = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 17)]
let size = CGSize(width: ui_view.frame.width,
height: 1000)
let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)
let estimateFrame = NSString(string: texto).boundingRect(with: size,
options: options,
attributes: l,
context: nil)
return estimateFrame.height
}
To use it, run this code:
let height = get_size_height_text(texto: label.text ?? "",
ui_view: self.view)
with this height, you need to increment the value with your preference to use it in your cell

Why is only image in first cell updated while the rest of the cells in uicollectionview not updated. ios swift

I'm trying to get 3 icons on the top bar.
import UIKit
class SectionHeader: UICollectionReusableView {
private let telButtonCellId = "TelButtonCell"
private let searchBarCellId = "SearchBarCell"
private let notificationButtonCellId = "NotificationButtonCell"
// MARK: - Properties
#IBOutlet weak var collectionView: UICollectionView!
// MARK: - Initialization
override func awakeFromNib() {
super.awakeFromNib()
self.collectionView.register(UINib(nibName:notificationButtonCellId, bundle: nil), forCellWithReuseIdentifier: notificationButtonCellId)
self.collectionView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.width*0.2) // this is need to set the size of the collection view
self.collectionView.delegate = self
self.collectionView.dataSource = self
}
extension SectionHeader : UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: notificationButtonCellId, for: indexPath) as! NotificationButtonCell
var image = UIImage(named: "globe")
cell.imageView.image=image?.addImagePaddingShift(x: 20 , y: 20,shiftX: 0, shiftY: 10)
cell.imageView.contentMode = .scaleAspectFit
cell.imageView.bounds=cell.bounds
cell.imageView.center = cell.center;
cell.backgroundColor = UIColor.lightGray
return cell
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let frameWidth = UIScreen.main.bounds.width
let frameHeight = UIScreen.main.bounds.width*0.2
return CGSize(width: frameWidth*0.15, height: frameHeight*1.0)
}
So only the first cell will have the globe displayed.
the rest of the 2 cells are empty even though i can change the background colour.
The SectionHeader is a nib
The imageView is subview of the cell, that needs to be with its size as I see. So it needs its frame (its position in relation to its superview coordinate system - the cell) to be set to the height and width of the cell with origin (0,0). The bounds of the cell provide these dimensions so the answer can be
cell.imageView.frame = cell.bounds
or better
cell.imageView.frame = CGRect(x: 0, y: 0, width: cell.frame.width, height: cell.frame.height)

Items in UICollectionView disappeared on scrolling

I have an UICollectionViewController which will be displayed both on its own full screen mode as well as in a pop-up view of another controller. The full screen mode works perfectly. However, in the pop-up mode, all items disappears after the user scroll the content up and down for a few times (sometimes more than 10 times, but also happen at the first or second time)
let vc = storyboard.instantiateViewController(
withIdentifier: "myCollectionView") as? MyColle ctionViewController
// set up the overlap in the parent controller
let bounds = CGRect(x: x + 22, y: parentFrame.origin.y + dy,
width: width, height: parentFrame.height - dy - 10)
overlayView = UIView(frame: bounds)
overlayView!.layer.cornerRadius = 5.0
overlayView!.backgroundColor = UIColor(red: 0.8, green: 0.8, blue: 0.8, alpha: 0.4)
self.view.addSubview(overlayView!)
vc.showAsPopup(overlayView!)
// in MyCollectionViewController, add itself to the parent popup view
func showAsPopup(_ parentView: UIView) {
view.backgroundColor = UIColor.red // test color to show the view is never gone
view.layer.cornerRadius = 5.0
collectionView?.backgroundColor = UIColor.clear
collectionView?.layer.cornerRadius = 5.0
parentView.addSubview(view)
view.frame = CGRect(x: 0, y: 20,
width: parentView.frame.width,
height: parentView.frame.height - 20)
NSLog("my view bounds after: \(view.frame.origin.x) \(view.frame.origin.y)")
NSLog(" : \(view.frame.width)x\(view.frame.height)")
}
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(_ collectionView: UICollectionView,
numberOfItemsInSection section: Int) -> Int {
NSLog("\(items.count) items in section \(section)")
return items.count
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
let screenRect = view.bounds
let scale = UIScreen.main.scale
let width = screenRect.size.width - 20
return CGSize(width: width, height: 45 * scale)
}
override func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let item = items[indexPath.item]
let cellId = "RCCell_\(item.id)"
NSLog("creating cell \(cellId)")
// Register cell classes
self.collectionView!.register(RemoteControlViewCell.self,
forCellWithReuseIdentifier: cellId)
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId,
for: indexPath) as! RemoteControlViewCell
// Configure the cell, override the icon in Cell
cell.itemId = item.id
cell.itemIcon.image = item.icon
cell.itemDescriptionLabel.text = item.description
return cell
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 12.5, left: 0, bottom: 12.5, right: 0)
}
override func collectionView(_ collectionView: UICollectionView,
didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
NSLog("removed cell at \(indexPath.row)")
}
// MyCollectionViewCell
override init(frame: CGRect) {
super.init(frame: frame)
let scale = UIScreen.main.scale
NSLog("Cell frame w: \(frame.size.width) h: \(frame.size.height), scale \(scale)")
let spacing = CGFloat(10)
contentView.backgroundColor = UIColor.white
contentView.layer.cornerRadius = 5
contentView.layer.masksToBounds = true
itemIcon = UIImageView(frame: CGRect(x: spacing, y: spacing,
width: CGFloat(24.0), height: CGFloat(24.0)))
itemIcon.contentMode = UIViewContentMode.scaleAspectFit
contentView.addSubview(itemIcon)
NSLog("icon w: \(itemIcon.frame.size.width)")
let labelX = spacing + 5 + itemIcon.frame.size.width
let font = UIFont.systemFont(ofSize: 20)
itemDescriptionLabel = UILabel(
frame: CGRect(x: labelX, y: spacing,
width: frame.size.width - labelX - spacing,
height: font.lineHeight))
itemDescriptionLabel.textColor = UIColor(colorLiteralRed: 0x66 / 255.0, green: 0x66 / 255.0, blue: 0x66 / 255.0, alpha: 1.0)
moduleDescriptionLabel.font = font
contentView.addSubview(itemDescriptionLabel)
NSLog("item desc \(String(describing: itemDescriptionLabel.text)) w: \(itemDescriptionLabel.frame.size.width)")
}
I tried changing the background color of the collectionView to visible and can see that the collect view itself was never gone even though the items disappeared. I also added logging to the collectionView() callback methods and found that they were never called again after the initial view presentation.
What could cause the items to disappear during scrolling?
Try using prepareForReuse in RemoteControlViewCell class
override func prepareForReuse() {
itemId = ""
itemIcon.image = nil
itemDescriptionLabel.text = ""
}

Resources