I've an issue when using the collectionView inside the tableViewCell XiB using diffable data source.Two sections I want to display like mens and womens.I don't know how to iterate the each case and display the record accordingly please check the code below.Passing the index path of tableView and using in collectionView for display the section on the basis of row.I don't know how i can create the correct datasource and snapshot. TableViewController Class:
public enum CategoriesSection{
case first
}
class TableCollectionCell: UIViewController {
@IBOutlet weak var tableView:UITableView!
var categories:[Product] = []
var mens1 :[Mens] = []
var womens1:[Womens] = []
var datasource:UITableViewDiffableDataSource<CategoriesSection,Product>!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = datasource
tableView.register(UINib(nibName: String(describing: TableCell.self), bundle: .main), forCellReuseIdentifier: String(describing: TableCell.self))
}
}
extension TableCollectionCell:UITableViewDelegate{
func createDataSource(){
datasource = UITableViewDiffableDataSource(tableView: tableView, cellProvider: { (tableview, indexPath, items) -> UITableViewCell? in
guard let cell = tableview.dequeueReusableCell(withIdentifier: String(describing: TableCell.self)) as? TableCell else {return UITableViewCell()}
cell.getData = self.categories[indexPath.row]
return cell
})
}
func createSnapshot(){
var snapshot = NSDiffableDataSourceSnapshot<CategoriesSection,Product>()
snapshot.appendSections([.first])
snapshot.appendItems(categories)
datasource.apply(snapshot, animatingDifferences: true, completion: nil)
}
}
CategoiresCell ViewController
public enum tableCollection{
case one
case two
case three
}
class CategoiresCell: UITableViewCell {
@IBOutlet weak var name:UILabel!
@IBOutlet weak var collectionView:UICollectionView!
var mens1 :[Mens] = []
var womens1:[Womens] = []
var row = 0
var datasource : UICollectionViewDiffableDataSource<tableCollection,String>?
override func awakeFromNib() {
super.awakeFromNib()
collectionView.delegate = self
collectionView.dataSource = datasource
collectionView.register(UINib(nibName: String(describing: MensCell.self), bundle: .main), forCellWithReuseIdentifier: String(describing: MensCell.self))
collectionView.register(UINib(nibName: String(describing: WomensCell.self), bundle: .main), forCellWithReuseIdentifier: String(describing: WomensCell.self))
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
extension CategoiresCell:UICollectionViewDelegate{
func createDataSource(){
datasource = UICollectionViewDiffableDataSource(collectionView: collectionView, cellProvider: { (collectionview, indexPath,items) -> UICollectionViewCell? in
switch self.row{
case 0:
guard let cell = collectionview.dequeueReusableCell(withReuseIdentifier: String(describing: MensCell.self), for: indexPath) as? MensCell else {return UICollectionViewCell()}
cell.getData = self.mens1[indexPath.item]
return cell
case 1:
guard let cell = collectionview.dequeueReusableCell(withReuseIdentifier: String(describing: WomensCell.self), for: indexPath) as? WomensCell else {return UICollectionViewCell()}
cell.getData = self.womens1[indexPath.item]
return cell
default:break
}
return UICollectionViewCell()
})
}
func createSnapshot(){
var snapshot = NSDiffableDataSourceSnapshot<tableCollection,String>()
// snapshot.appendSections([.first])
// snapshot.appendItems(products)
// datasource.apply(snapshot, animatingDifferences: true, completion: nil)
//
}
}