gpt4 book ai didi

swift - 如何使用 UIViewControllerRepresentable 在 SwiftUI 中呈现 UICollectionView

转载 作者:行者123 更新时间:2023-12-03 19:13:28 52 4
gpt4 key购买 nike

首先,我知道可以使用 SwiftUI 列表等选项来获得类似的效果。但是我需要 UICollectionView 的自动滚动功能,所以我真的很想实现一个“老派”版本。我什至不想要理想的组合布局版本。

我当前的代码如下所示:

import SwiftUI

struct CollectionView: UIViewControllerRepresentable {

private var isActive: Binding<Bool>
private let viewController = UIViewController()
private let collectionController: UICollectionView

init(_ isActive: Binding<Bool>) {
self.isActive = isActive
self.collectionController = UICollectionView(frame: CGRect(x: 0, y: 0, width: 100, height: 200), collectionViewLayout: UICollectionViewFlowLayout())
}
func makeUIViewController(context: UIViewControllerRepresentableContext<CollectionView>) -> UIViewController {
return viewController
}

func updateUIViewController(_ uiViewController: UIViewController, context: UIViewControllerRepresentableContext<CollectionView>) {
if self.isActive.wrappedValue && collectionController.delegate == nil { // to not show twice
collectionController.delegate = context.coordinator
collectionController.dataSource = context.coordinator
}
}

func makeCoordinator() -> Coordintor {
return Coordintor(owner: self)
}

final class Coordintor: NSObject, UICollectionViewDelegate, UICollectionViewDataSource {
weak var viewController:UIViewController?
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}
var cellId = "Cell"
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
return cell
}
// works as delegate
let owner: CollectionView
init(owner: CollectionView) {
self.owner = owner

}
}
}

不幸的是,我得到的只是预览中的空白屏幕。现在,如果我可以只显示大量的红色方块选择,我可以滚动浏览并在出现时自动滚动到底部,那将是理想的。

谢谢!

最佳答案

这是最小的可运行演示。 (注意:如果全部以编程方式完成,则必须注册单元格)

demo

class MyCell: UICollectionViewCell {
}

struct CollectionView: UIViewRepresentable {
func makeUIView(context: Context) -> UICollectionView {
let view = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
view.backgroundColor = UIColor.clear
view.dataSource = context.coordinator

view.register(MyCell.self, forCellWithReuseIdentifier: "myCell")
return view
}

func updateUIView(_ uiView: UICollectionView, context: Context) {
}

func makeCoordinator() -> Coordinator {
Coordinator()
}

class Coordinator: NSObject, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
3
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "myCell", for: indexPath) as! MyCell
cell.backgroundColor = UIColor.red
return cell
}
}
}

struct TestUICollectionView_Previews: PreviewProvider {
static var previews: some View {
CollectionView()
}
}

关于swift - 如何使用 UIViewControllerRepresentable 在 SwiftUI 中呈现 UICollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61424672/

52 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com