我在使用可区分数据源的 tableViewCell XiB 中使用 collectionView 时遇到问题。我想像男装和女装一样显示两个部分。我不知道如何迭代每个案例并相应地显示记录请检查下面的代码.传递tableView的索引路径并在collectionView中使用以显示基于行的部分。我不知道如何创建正确的数据源和快照。表视图控制器类:
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)
}
}
类别单元视图控制器
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)
//
}
}