Sunday, November 8, 2026
HomeiOS Developmentios - Preselection of an merchandise on LazyHGrid

ios – Preselection of an merchandise on LazyHGrid

[ad_1]

Hi everybody I am working with a LazyHGrid
The grid reveals 19 cells divided on 2 strains and their contents are instances that the person can choose.

I’ve an issue with the preselection, i.e. I would love that when the horizontal grid is proven, a preselection based mostly on the present time is seen for instance

I will provide you with an instance of the grid


| 9:30 | – | 10:00 | – | 10:30 | – | 11:00 |


If presently it’s 10:20 am I would love the cell containing the textual content 10:30 to be routinely chosen when the person sees the grid for the primary time


| 9:30 | – | 10:00 | – | 10:30 | – | 11:00 |
Preselection at 10:30 if present time is 10:20


In a nutshell I would wish the number of the cell that comprises a time half an hour forward of the present time

That is what I’ve finished thus far

Are you able to assist me?

struct TimeSelectorView: View {
    
    @State non-public var selectedItem: Int = 0
    
    @Binding var date: Date
    
    var physique: some View {
        
        VStack (alignment: .heart, spacing: 20) {
            ScrollView(.horizontal, showsIndicators: false) {
                ScrollViewReader { reader in
                    LazyHGrid(rows: Array(repeating: GridItem(.adaptive(minimal: 100, most: .infinity), spacing: 0), depend: 2), spacing: 0) {
                        ForEach(reservationTimeItems.indices) { worth in
                        
                            TimeSelectorCellView(selectedItem: $selectedItem, date: $date, hours: reservationTimeItems[value].hour, minute: reservationTimeItems[value].minute, merchandise: worth)
                                
                                .body(width: (display.dimension.width - 20 ) / 4)
                                .padding([.top, .bottom], 10)
                                .id(worth)
                        }
                    }
                    .onAppear { scrollTo((8, 12, 0), reader) }
                    .onChange(of: date) { _ in scrollTo((8, 12, 0), reader) }
                }
            }
            .body(peak: 110)
        }
    }
}


struct TimeSelectorCellView: View {
       
    @Binding var selectedItem: Int
    @Binding var date: Date
    
    non-public var selectionColor: Shade { chosen ? .greenApp : .white }
    non-public var selectionOpacity: CGFloat { chosen ? 1 : 0 }
    
    non-public var chosen: Bool {
        return hours == Calendar.present.part(.hour, from: date.nextHalfHour()) &&
        minute == Calendar.present.part(.minute, from: date.nextHalfHour())
    }
    
    non-public var disableItem: Bool 
    
    var hours: Int // Ore della knowledge
    var minute: Int // Minuti della knowledge
    var merchandise: Int

    var physique: some View {
        
        VStack(alignment: .main, spacing: 10) {
            
            HStack(alignment: .heart, spacing: 2) {
                                
                Textual content(hours < 10 ? "0(hours)" : "(hours)")
                    .foregroundColor(disableItem ? disabledColor : selectionColor)
                    .font(.system(dimension: 23))
                
                // Minuti
                HStack(spacing: 3) {
                    Textual content(":").foregroundColor(.white)
                    Textual content(minute < 10 ? "0(minute)" : "(minute)")
                        .foregroundColor(disableItem ? disabledColor : selectionColor)
                }
                .font(.footnote)
            }
        }
        // Selezione e suggestions tattile
        .onTapGesture {
            UIImpactFeedbackGenerator(type: .medium).impactOccurred()

            DispatchQueue.essential.async {
                withAnimation(.easeInOut(length: animationDuration)) {
                    // Gestisce la selezione della cella
                        selectedItem = merchandise
                }
            }

            date = Calendar.present.date(bySettingHour: hours, minute: minute, second: 0, of: date)!
        }
        .disabled(disableItem)
    }
}

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments