TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Trying to save the last index in a variable to perform automatic scrolling

Forums > SwiftUI

I have created a horizontal scroll view containing many items. I am trying to, every time I update or delete an item, to save the item's index, so that an automatic scrolling can be performed to the position where the the item is or was. What I am struggling to understand is why I can't assign the index to the variable lastItemIndex. Could you please check what is wrong with my code and how to make it work correctly?

struct ItemsCollectionView: View {
    @ObservedObject var itemsDataSource: ItemsDataSource
    @State private var lastItemIndex: Int = 0

    public var body: some View {
        ScrollViewReader { scrollView in

            HStack {
                ForEach(Array(itemsDataSource.items.enumerated()), id: \.offset) { index, item in
                    ItemView(
                        content: ItemViewContentProvider.content(
                            item: item,
                            onItemTapped: { item in
                                self.assignIndex(index)
                                itemsDataSource.add(item)
                            },
                            onDelete: { item in
                                self.assignIndex(index)
                                itemsDataSource.remove(item)
                            })
                    )
                }
            }.onAppear {
                scrollView.scrollTo(lastItemIndex)
            }
        }
    }

    func assignIndex(_ index: Int) {
        lastItemIndex = index
    }
}

struct ItemView: View {
    let content: ItemViewContent

    var body: some View {
        HStack {
            Button(action: {
                content.onitemTapped(content.item)
            }, label: {
                Text(content.itemTitle)
                    .font(.copy)
                    .foregroundColor(.contentLead)
            })
            Button(action: {
                content.onDelete(content.item)
            }, label: {
                Image(uiImage: .icons.close)
                    .resizable()
                    .frame(width: 10, height: 10)
                    .foregroundColor(.contentSecondary)
            })
        }
        .padding(.horizontal, 8)
        .background(Rectangle()
            .foregroundColor(.backgroundSecondary)
            .cornerRadius(4)
            .frame(height: 30))
        .frame(height: 30)
    }
}

3      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

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.