I am implementing a collection view cell inside a table view cell but strange things are happening with the collection view indexPath.
The numberOfItemsinSection gets fired 3 times and then cellForItemAt only once when numberOfitemsInSection is 2. I expect one section and 2 cells for the collection view.
Here is the source data:
[["myDomain.jpg1", "myDomain.jpg2"]]
Here is the console output from the following code:
"number of items in section: 2"
"number of items in section: 2"
"number of items in section: 2"
"cell for item at [0, 0]"
"indexPath.section: 0"
"indexPath.row: 0"
"myDomain.jpg1"
Here is my view controller:
class WishListsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var tableView: UITableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
tableView.register(WishListsCell.self, forCellReuseIdentifier: cellId)
self.view.addSubview(tableView)
}
func numberOfSections(in tableView: UITableView) -> Int {
return wishLists.count // 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! WishListsCell
return cell
}
Here is my table view cell:
class WishListsCell: UITableViewCell {
var collectionView: UICollectionView!
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
layout.itemSize = CGSize(width: screenWidth, height: (screenWidth / 4))
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
layout.estimatedItemSize.height = (screenWidth / 4)
layout.estimatedItemSize.width = screenWidth
collectionView = UICollectionView(frame: contentView.frame, collectionViewLayout: layout)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.register(WishListsCollectionViewCell.self, forCellWithReuseIdentifier: cellId)
self.contentView.addSubview(collectionView)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
extension WishListsCell: UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
let items = wishListUrls[section]
debugPrint("number of items in section: \(items.count)")
return items.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! WishListsCollectionViewCell
debugPrint("cell for item at \(indexPath)")
debugPrint("indexPath.section: \(indexPath.section)")
debugPrint("indexPath.row: \(indexPath.row)")
if let imageUrl = wishListUrls[indexPath.section][indexPath.row] as String! {
debugPrint(imageUrl)