'Dropwn list menu open behind of other Views in SwiftUi

When I make dropdown list menu in SwiftUi, dropdown list shows behind of other Component View, I've tried to zIndex(1) at the last of the VStack, and I've tried .overlay at the top of the Stack but it didn't solve my problem, I've shared below code and I've shared screenshot of the problem, how can I solve this problem? thanks...

enter image description here

import SwiftUI

    struct DropdownOption: Hashable {
        let key: String
        let value: String
        
        public static func == (lhs: DropdownOption, rhs: DropdownOption) -> Bool {
            return lhs.key == rhs.key
        }
    }
    
    struct DropdownRow: View {
        var option: DropdownOption
        var onOptionSelected: ((_ option: DropdownOption) -> Void)?
        
        var body: some View {
            Button(action: {
                if let onOptionSelected = self.onOptionSelected {
                    onOptionSelected(self.option)
                }
            }) {
                HStack {
                    Text(self.option.value)
                        .font(.system(size: 14))
                        .foregroundColor(Color.black)
                    Spacer()
                }
            }
            .padding(.horizontal, 16)
            .padding(.vertical, 5)
        }
    }
    
    struct Dropdown: View {
        var options: [DropdownOption]
        var onOptionSelected: ((_ option: DropdownOption) -> Void)?
        
        var body: some View {
            ScrollView {
                VStack(alignment: .leading, spacing: 0) {
                    ForEach(self.options, id: \.self) { option in
                        DropdownRow(option: option, onOptionSelected: self.onOptionSelected)
                    }
                }
            }
            .frame(minHeight: CGFloat(options.count) * 30, maxHeight: 250)
            .padding(.vertical, 5)
            .background(Color.white)
            .cornerRadius(5)
            .overlay(
                RoundedRectangle(cornerRadius: 5)
                    .stroke(Color.gray, lineWidth: 1)
            )
        }
    }
    
    struct DropdownSelector: View {
        @State private var shouldShowDropdown = false
        @State private var selectedOption: DropdownOption? = nil
        var placeholder: String
        var options: [DropdownOption]
        var onOptionSelected: ((_ option: DropdownOption) -> Void)?
        private let buttonHeight: CGFloat = 45
        
        var body: some View {
            Button(action: {
                self.shouldShowDropdown.toggle()
            }) {
                HStack {
                    Text(selectedOption == nil ? placeholder : selectedOption!.value)
                        .font(.system(size: 14))
                        .foregroundColor(selectedOption == nil ? Color.gray: Color.black)
                    
                    Spacer()
                    
                    Image(systemName: self.shouldShowDropdown ? "arrowtriangle.up.fill" : "arrowtriangle.down.fill")
                        .resizable()
                        .frame(width: 9, height: 5)
                        .font(Font.system(size: 9, weight: .medium))
                        .foregroundColor(Color.black)
                }
            }
            .padding(.horizontal)
            .cornerRadius(5)
            .frame(width: .infinity, height: self.buttonHeight)
            .overlay(
                RoundedRectangle(cornerRadius: 5)
                    .stroke(Color.gray, lineWidth: 1)
            )
            .overlay(
                VStack {
                    if self.shouldShowDropdown {
                        Spacer(minLength: buttonHeight + 10)
                        Dropdown(options: self.options, onOptionSelected: { option in
                            shouldShowDropdown = false
                            selectedOption = option
                            self.onOptionSelected?(option)
                        })
                    }
                }, alignment: .topLeading
            )
            .background(
                RoundedRectangle(cornerRadius: 5).fill(Color.white)
            )
        }
    }

calling DropDownList

ZStack(alignment:.top){
                    HStack {
                        Group {
                            DropdownSelector(
                                placeholder: "Choose Aircraft Type",
                                options: options,
                                onOptionSelected: { option in
                                    print(option)
                                })
                                .padding(.horizontal)
                            
                        }
                    }.padding(.top, 50)
                    
                    HStack {
                        Group {
                            DropdownSelector(
                                placeholder: "Choose Simulator Type",
                                options: optionsSimulator,
                                onOptionSelected: { option in
                                    print(option)
                                })
                                .padding(.horizontal)
                            
                        }
                    }.padding(.top, 50)
}


Solution 1:[1]

I suggest to take a course in basic C programming before continuing with implementing a hash table. There are numerous errors and just generic weird things, of the top of my hat:

  • you are initializing an int (key) to NULL, which is a pointer.
  • you are initializing 'hashFunction' elements in a 'tableSize' array (wrong variable?)
  • you are only storing the first character of your string as a key.
  • the if statement where you check if your key is equal to the first character of your string, and, if it is, set it again to be equal to that, just in case before returning is ridiculous.
  • using 2 loop indices (both h and i seems superfluous).

Beyond these basic problems, the problem you are asking about is that chain will be NULL. If you use ...chain->data, you will dereference a NULL pointer, which is bad.

The real problem is that you don't seem to understand linked lists nor C memory management, which are a prerequisite for implementing more complicated structures, such as hash tables, so I would suggest you take a step back and start with a basic C course before continuing like this.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jurjen