我有以下代碼,但我不確定將它放在我的 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
}
}
}
當視圖出現時,我需要滾動到該位置。
uj5u.com熱心網友回復:
您可以使用ViewControllerRepresentable并利用視圖控制器可以覆寫viewWillAppear/viewDidAppear方法的事實,您可以在其中撰寫滾動代碼。
為此, subclass 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) {
}
}
uj5u.com熱心網友回復:
一個場景并不完全清楚,但可以使用外部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://www.uj5u.com/houduan/489139.html
標籤:迅速 迅捷 uicollectionview uiviewrepresentable
