[英]How to make two collection views in one view?
I need to make either two collection views or one collection view with two sections.
我需要制作两个集合视图或一个包含两个部分的集合视图。 It didn't work for me through flow, I decided to use the composition layout.
它通过流程对我不起作用,我决定使用组合布局。 but all the same nothing comes out.
但仍然没有任何结果。
Perhaps this can be done with a regular flow collectionview
.也许这可以通过常规流
collectionview
来完成。 I would have the easiest option, because the code below is practically not mine, I found it on the Internet我会有最简单的选择,因为下面的代码实际上不是我的,我在互联网上找到的
I just need two sections, one with currency, the other with categories.我只需要两个部分,一个是货币,另一个是类别。
Error on the screen屏幕上的错误
struct News: Hashable {
var name: String
var id: Int
func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
static func == (lhs: News, rhs: News) -> Bool {
return lhs.id == rhs.id
}
}
class ViewController: UIViewController {
var currency = Currency.getCurrency()
var category = Categories.getCategory()
var collectionView: UICollectionView!
enum Section: Int, CaseIterable {
case currency, category
}
var dataSource: UICollectionViewDiffableDataSource<Section, News>?
override func viewDidLoad() {
super.viewDidLoad()
setupCollectionView()
createDataSource()
reloadData()
}
private func setupCollectionView() {
let collectionView = UICollectionView(frame: CGRect(x: 0, y: 100, width: view.frame.width, height: 200), collectionViewLayout: createCompositionLayout())
//collectionView.autoresizingMask = [.flexibleWidth]
collectionView.backgroundColor = .white
view.addSubview(collectionView)
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "currencyCell")
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "categoriesCell")
}
private func reloadData() {
var snapshot = NSDiffableDataSourceSnapshot<Section, News>()
snapshot.appendSections([.currency, .category])
snapshot.appendItems(currency, toSection: .currency)
snapshot.appendItems(category, toSection: .category)
dataSource?.apply(snapshot, animatingDifferences: true)
}
}
extension ViewController {
private func createDataSource() {
dataSource = UICollectionViewDiffableDataSource<Section, News>(collectionView: collectionView, cellProvider: { (collectionView, indexPath, section) -> UICollectionViewCell? in
guard let section = Section(rawValue: indexPath.section) else { fatalError("Unknown section kind")
}
switch section {
case .currency:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "currencyCell", for: indexPath)
cell.backgroundColor = .red
return cell
case .category:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "categoryCell", for: indexPath)
cell.backgroundColor = .blue
return cell
}
})
}
private func createCompositionLayout() -> UICollectionViewLayout {
let layout = UICollectionViewCompositionalLayout { (sectionIndex, layoutEnvironment) -> NSCollectionLayoutSection? in
guard let section = Section(rawValue: sectionIndex) else {
fatalError("Unknown section kind")
}
switch section {
case .currency:
return self.createCurrency()
case .category:
return self.createCategory()
}
}
return layout
}
private func createCurrency() -> NSCollectionLayoutSection {
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1),
heightDimension: .fractionalHeight(1))
let item = NSCollectionLayoutItem(layoutSize: itemSize)
let groupSize = NSCollectionLayoutSize(widthDimension: .absolute(60),
heightDimension: .absolute(40))
let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])
let section = NSCollectionLayoutSection(group: group)
section.interGroupSpacing = 20
section.contentInsets = NSDirectionalEdgeInsets.init(top: 16, leading: 20, bottom: 0, trailing: 20)
section.orthogonalScrollingBehavior = .continuous
return section
}
private func createCategory() -> NSCollectionLayoutSection {
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1),
heightDimension: .fractionalHeight(1))
let item = NSCollectionLayoutItem(layoutSize: itemSize)
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.3),
heightDimension: .absolute(40))
let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])
let section = NSCollectionLayoutSection(group: group)
section.interGroupSpacing = 8
section.contentInsets = NSDirectionalEdgeInsets.init(top: 16, leading: 20, bottom: 0, trailing: 20)
section.orthogonalScrollingBehavior = .continuous
return section
}
}[![enter image description here][1]][1]
You can use UITableView and in UITableViewCell use UICollectionView, you can have UICollectionView as many as you want in your UIViewController.您可以使用 UITableView 并在 UITableViewCell 中使用 UICollectionView,您可以在 Z11156C35B03DBCA74 中拥有任意数量的 UICollectionView。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.