GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

scrollTo not working with List

Forums > SwiftUI

Can someone tell me what I am doing wrong. When running, I see that it should scrollTo my item, but it doesn't.

struct ListOfPermitInspectionsView: View {
    @Environment(\.modelContext) private var modelContext
    @Environment(PT_CurrentUser.self) private var activeUser
    @Environment(DBPermitEventModel.self) private var permitEvent : DBPermitEventModel?
    @Environment(DBPermitMainModel.self) private var permit : DBPermitMainModel
    @EnvironmentObject var permitViewActions : PermitViewActions

    @State private var selectedItem : DBPermitInspectionModel?
    @Query var inspectionItems : [DBPermitInspectionModel]

    init(predicate: Predicate<DBPermitInspectionModel>)
    {
        _inspectionItems = Query(filter: predicate, sort: [SortDescriptor(\DBPermitInspectionModel.inspectionID)])
    }

    var body: some View {

        VStack{
            HStack{
                Text("INSPECTIONS")
                    .padding(5)
                Spacer()
            }
            .blueTitleStyle()

            ScrollViewReader{ proxy in

                List{
                    ForEach(inspectionItems) {item in
                        VStack{
                            InspectionItemView(inspectionItem: item)
                        }
                        .listRowBackground(
                            selectedItem?.inspectionID == item.inspectionID ? Color.SELECTCOLOR : Color.clear
                        )
                        .onTapGesture {
                            selectedItem = item
                        }
                        //.id(item)
                    }
                }
                .listStyle(.plain)
                .onChange(of: selectedItem, { oldValue, newValue in
                    if oldValue == nil && newValue != nil {
                        print("Should Scroll To Item: \(newValue!.inspectionID)")
                            withAnimation {
                                proxy.scrollTo(selectedItem, anchor: .center)
                            }
                    }
                    permitViewActions.inspectionItemSelected = newValue
                })
                .onReceive(permitViewActions.$inspectionItemScrollTo){ item in
                    if let scrollToItem = permitViewActions.inspectionItemScrollTo
                    {
                        print("onReceive Scroll to Item: \(scrollToItem.inspectionID)")
                        selectedItem = scrollToItem
                        permitViewActions.inspectionItemScrollTo = nil
                        withAnimation {
                            proxy.scrollTo(selectedItem, anchor: .center)
                        }
                    }
                }
                .onAppear{
                    print("ON APPEAR")
                    if selectedItem == nil{
                        if let permitEvent = permitEvent {
                            selectedItem = PT_PermitEvents.getSelectedInspectionByID(inspectionID: permitEvent.eventInspectionID, userId: activeUser.userID, permitId: permit.permitID, modelContainer: modelContext.container)
                            print("This is an event and the selectedItem is: \(selectedItem?.inspectionID ?? "NOTHING!!!")")
                        }
                        else{
                            let inspectionPredicate = PT_PermitEvents.getEventInspectionsPredicate(USERID: activeUser.userID, CONID: activeUser.CONID, PERMITID: permit.permitID)
                            let fetchDesc = FetchDescriptor(predicate: inspectionPredicate, sortBy: [SortDescriptor(\DBPermitInspectionModel.inspectionID)])
                            if let firstInspection = try? modelContext.fetch(fetchDesc).first
                            {
                                selectedItem = firstInspection
                            }
                            print("This is a permit only and the selectedItem is: \(selectedItem?.inspectionID ?? "NOTHING!!!")")
                        }
                    }
                }
            }
        }
    }

}

   

If I remember rightly, you need to give your items an id to allow them to be scrolled to. Check this out:

https://www.hackingwithswift.com/quick-start/swiftui/how-to-make-a-scroll-view-move-to-a-location-using-scrollviewreader

Steve

   

Go further, faster with the Swift Career Accelerator.

GO FURTHER, FASTER Unleash your full potential as a Swift developer with the all-new Swift Career Accelerator: the most comprehensive, career-transforming learning resource ever created for iOS development. Whether you’re just starting out, looking to land your first job, or aiming to become a lead developer, this program offers everything you need to level up – from mastering Swift’s latest features to conquering interview questions and building robust portfolios.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.