I'm having the same problem as @lozzoc, my view controller is a standard UIViewController, there is a UICollectView on the page along with other UI elements.
I have added an extension for my view controller that implements UICollectionViewDataSource
// MARK: - UICollectionViewDataSource
func numberOfSections(in collectionView: UICollectionView) -> Int {
if #available(iOS 14.0, *) {
os_log(.debug, "UICollectionViewDataSource: numberOfSections: 1")
}
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if #available(iOS 14.0, *) {
os_log(.debug, "UICollectionViewDataSource: numberOfItemsInSection: \(self.searchTerms.count)")
}
return searchTerms.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if #available(iOS 14.0, *) {
os_log(.debug, "UICollectionViewDataSource: cellForItemAt: \(indexPath)")
}
return cell(from: collectionView, for: indexPath)
}
func cell(from collectionView: UICollectionView, for indexPath: IndexPath) -> SearchTermCell {
if #available(iOS 14.0, *) {
os_log(.debug, "RecomendedPageContentViewController: for \(indexPath)")
}
func extractViewModel(at indexPath: IndexPath) -> SearchTermCell.ViewModel {
if #available(iOS 14.0, *) {
os_log(.debug, "extractViewModel: at \(indexPath)")
}
let viewModel: SearchTermCell.ViewModel
let value = getData(for: indexPath)
viewModel = self.viewModel(for: value)
return viewModel
}
let cell: SearchTermCell = collectionView.dequeueReusableCell(withReuseIdentifier: "SearchTermCell", for: indexPath) as! SearchTermCell
configure(cell: cell, with: extractViewModel(at: indexPath))
cell.setNeedsLayout()
cell.sizeToFit()
return cell
}
I included cell(from collectionView: UICollectionView, for indexPath: IndexPath), but cellForItemAt is never called.
The collection view is connected to the view controller (via a Storyboard)
@IBOutlet weak var collectionView: UICollectionView!
in viewDidLoad the view controller is assigned as the datasource
self.collectionView.dataSource = self
In the storyboard the UICollectViewCell has its class set to my custom UICollectionViewCell
class SearchTermCell: UICollectionViewCell {
@IBOutlet private weak var searchLabel: UILabel!
var viewModel = ViewModel() {
didSet {
backgroundColor = UIColor.appColor(.title)
searchLabel.text = viewModel.title
searchLabel.font = .titleFont
searchLabel.textColor = UIColor.appColor(.cellBackColour)
if #available(iOS 14.0, *) {
os_log(.debug, """
-----------------------------
searchLabel.text: \(self.viewModel.title)
-----------------------------
""")
}
}
}
}
Any ideas would be appreciated.