如何解决如何在一个视图中创建两个集合视图?
也许这可以通过常规流程 collectionview
来完成。我会有最简单的选择,因为下面的代码实际上不是我的,我是在网上找到的
我只需要两个部分,一个是货币,另一个是类别。
屏幕错误
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),subitems: [item])
let section = NSCollectionLayoutSection(group: group)
section.interGroupSpacing = 8
section.contentInsets = NSDirectionalEdgeInsets.init(top: 16,trailing: 20)
section.orthogonalScrollingBehavior = .continuous
return section
}
}[![enter image description here][1]][1]
解决方法
您可以使用 UITableView,在 UITableViewCell 中使用 UICollectionView,您可以在 UIViewController 中拥有任意数量的 UICollectionView。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。