我有下面的代码,但是我不知道将它放在我的UIViewRepresntable中的位置。有什么建议吗?
let scrollTo = IndexPath(row: 25, section: 0)
view.scrollToItem(at: scrollTo, at: .top, animated: false)
struct CollectionViewRepresentable: UIViewRepresentable {
@StateObject var vm = CollectionViewModel()
let view = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewCompositionalLayout.list(using: UICollectionLayoutListConfiguration(appearance: .plain)))
func makeUIView(context: Context) -> UICollectionView {
view.backgroundColor = UIColor.clear
view.dataSource = context.coordinator
view.delegate = context.coordinator
view.register(ListCell.self, forCellWithReuseIdentifier: "listCell")
return view
}
func updateUIView(_ uiView: UICollectionView, context: Context) {
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject, UICollectionViewDelegate, UICollectionViewDataSource {
private var parent: CollectionViewRepresentable
init(_ collectionViewRepresentable: CollectionViewRepresentable) {
self.parent = collectionViewRepresentable
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return parent.vm.items.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! ListCell
let item = parent.vm.items[indexPath.row]
cell.item = item
return cell
}
}
}当视图出现时,我需要滚动到那个位置。
发布于 2022-06-11 04:55:26
您可以使用ViewControllerRepresentable,并利用视图控制器可以覆盖viewWillAppear/viewDidAppear方法这一事实,在该方法中,可以编写滚动代码。
为此,子类UICollectionViewController,并将所有集合视图相关逻辑移到:
class MyCollectionViewController: UICollectionViewController {
var vm = CollectionViewModel()
override func viewDidLoad() {
super.viewDidLoad()
collectionView.backgroundColor = UIColor.clear
collectionView.register(ListCell.self, forCellWithReuseIdentifier: "listCell")
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let scrollTo = IndexPath(row: 25, section: 0)
collectionView.scrollToItem(at: scrollTo, at: .top, animated: false)
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return vm.items.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! ListCell
let item = vm.items[indexPath.row]
cell.item = item
return cell
}
}考虑到上述情况,SwiftUI代码简化为如下所示:
struct CollectionViewRepresentable: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> MyCollectionViewController {
.init(collectionViewLayout: UICollectionViewFlowLayout())
}
func updateUIViewController(_ vc: MyCollectionViewController, context: Context) {
}
}发布于 2022-06-11 04:20:25
场景还不完全清楚,但可以在可表示的内部使用外部State和Binding,并通过调用updateUIView进行更改。
所以一个可能的解决办法是
@State private var scrollTo = IndexPath(row: 25, section: 0) // << in parent !!
// ...
struct CollectionViewRepresentable: UIViewRepresentable {
@Binding var scrollTo: IndexPath // << 1) !!
@StateObject private var vm = CollectionViewModel()
func makeUIView(context: Context) -> UICollectionView {
let view = UICollectionView(...) // << move creation here !!
// ... other code
return view
}
func updateUIView(_ uiView: UICollectionView, context: Context) {
// called at start and when binding updated
uiView.scrollToItem(at: scrollTo, at: .top, animated: false) // << 2) !!
}
// ... other code
}https://stackoverflow.com/questions/72580985
复制相似问题