UIViewRepresentable中的UICollectionView赢得';t从一个表中滚动到一个项目

UIViewRepresentable中的UICollectionView赢得';t从一个表中滚动到一个项目,uicollectionview,swiftui,Uicollectionview,Swiftui,我有一个类,用于在成员变量UICollectionView中触发移动函数: class CollectionFunctionStore : ObservableObject { //class to hold data about a UICollection and from where some functions can be triggered var collectionRef : UICollectionView? func jumpGrid(){

我有一个类,用于在成员变量UICollectionView中触发移动函数:

class CollectionFunctionStore : ObservableObject {
    //class to hold data about a UICollection and from where some functions can be triggered
    var collectionRef : UICollectionView?

    func jumpGrid(){
        if let lclCollection = collectionRef {
        let ip = IndexPath(row: 15, section: 0)
        lclCollection.scrollToItem(at: ip, at: .bottom, animated: false)
        lclCollection.reloadData()
        print("jumpGrid() was reached: ")
        }
    }
}
这里的单一功能“jumpGrid”会将网格反弹到第15行 此类正在swiftUI应用程序中使用,相关集合包含在UIViewRepresentable中。函数存储区和包含UICollectionView的UIViewRepresentable都与顶级内容视图中的按钮一起位于结构中

import SwiftUI

struct ContentView: View {
    @ObservedObject var swiperStore = CollectionFunctionStore()
    var body: some View {
       return ZStack {
        VStack{
            CollectionAndStoreContainer(swiperStore: swiperStore)
            JumpButton(swiperStore: swiperStore)
        }.onAppear(perform:  swiperStore.jumpGrid)
       }
    }
}

struct CollectionAndStoreContainer : View {
    @ObservedObject var swiperStore : CollectionFunctionStore
    var body: some View{
        return ZStack{
            CollectionViewRepresentable(swiperStoreParam: swiperStore).frame(width: 30, height: 200)
        }
    }
}

struct JumpButton : View {
    @ObservedObject var swiperStore : CollectionFunctionStore
    var body : some View{
        return Button(action: {
            swiperStore.jumpGrid()
        }){
            ZStack {
                Rectangle().frame(width: 60, height: 30).foregroundColor(.orange).cornerRadius(4)
                Text("j").foregroundColor(.white)
            }
        }
    }
}
我面临的问题是:包含与顶层视图相同的函数存储的按钮可以触发滚动到项目功能,而onAppear事件可以调用该函数,它将实际打印其代码中包含的消息,但不会移动集合的内容。。。 按钮调用该函数,UICollectionView会正确响应 onAppear调用函数,但UICollectionView不移动

======一些其他信息===== 在这种情况下,我没有选择使用UIViewRepresentable,我不能使用lazyHgrid或HStacks 此外,这里还有相关UIViewRepresentable的代码

struct CollectionViewRepresentable : UIViewRepresentable {
    
    var swiperStore : CollectionFunctionStore
    
    typealias UIViewType = UICollectionView
    
    init(swiperStoreParam : CollectionFunctionStore) {
        swiperStore = swiperStoreParam
    }
    
    func makeUIView(context: Context) -> UICollectionView {
        
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        layout.itemSize = CGSize(width: 30, height: 30)
        layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        layout.minimumLineSpacing = 0
       
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.dataSource = context.coordinator
        collectionView.delegate = context.coordinator
        collectionView.register(ExtendedCell.self, forCellWithReuseIdentifier: "ExtCell")
        collectionView.isScrollEnabled = true
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.showsVerticalScrollIndicator = false
        swiperStore.collectionRef = collectionView
        return collectionView
    }
    
    func makeCoordinator() -> Coordinator {
            return Coordinator()
    }
    
    func updateUIView(_ uiView: UICollectionView, context: Context) {
         
    }
}
有人有什么想法吗?总体目标是让UICollectionView在它出现的一开始就滚动到一个特定的位置,所以我认为在SwiftUI中使用onAppear是最好的方式,但是由于某些原因它没有移动网格,例如在协调器中有更好的选项吗?
感谢您的帮助。

我认为现在在onAppear中调用scroll to还为时过早,因为集合视图当时还没有完成构建

试一试

甚至

}.onAppear {
 // with some delay, if appeared constructed with animation or async, etc.
 DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {  // 0.25-0.5 sec
    swiperStore.jumpGrid() 
 }
}

只要把它放进去,我就得到了“表达式解析为一个未使用的函数”的消息,……还有更好的生命周期事件可以使用吗?啊!,我犯了一个语法错误,我在快速更改后尝试了第一个,它似乎起了作用。告诉我,在UIViewRepresentable的协调器中有没有这样做的方法?哈哈,我自己应该得到的…有点尴尬。。。
}.onAppear {
 // with some delay, if appeared constructed with animation or async, etc.
 DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {  // 0.25-0.5 sec
    swiperStore.jumpGrid() 
 }
}