HierarchyView

public struct HierarchyView
extension HierarchyView: View
extension HierarchyView: _ViewEmptyChecking

HierarchyView displays tree-structured data using HierarchyItemView rows and an optional HierarchyHeader, which is available for customization purposes.

Overview

Use HierarchyView when you need to browse, navigate, and (optionally) select items in a hierarchical data set (parent/child relationships). The component delegates data access to a HierarchyViewDataSource so large or dynamic trees can be served efficiently.

Key Features

  • Pluggable data source (HierarchyViewDataSource) defining root, children, parent lookups, and titles.
  • Custom per-item view content via the hierarchyItem closure.
  • Optional header view for customized navigation controls / summary information.
  • Built-in selection handling (single / multiple / none) controlled by the hierarchyItemSelectionMode environment value.
  • Style system (Fiori & custom) applied through hierarchyViewStyle modifiers.

Data Source Contract (Summary)

Your data source must provide stable, unique String identifiers for every item. Child counts and IDs should remain consistent during a single render pass. See HierarchyViewDataSource for full protocol requirements.

struct HierarchySimpleDataSource: HierarchyViewDataSource {
    func rootID() -> String {
        return "100"
    }

    func numberOfChildren(for id: String) -> Int {
        return Int.random(in: 0...5)
    }

    func childID(idForChildItemAt index: Int, with parentID: String) -> String {
        if let intValue = Int(parentID) {
            return String(intValue + index)
        } else {
            return ""
        }
    }

    func parentID(for id: String) -> String? {
        if let intValue = Int(id), intValue > 100 {
            return String(intValue - 100)
        } else {
            return nil
        }
    }

    func itemTitle(for id: String) -> String? {
        return id
    }
}

State Bindings

  • activeChildItem: The identifier that will become active (e.g. next navigated child) when the user invokes forward navigation in the header.
  • selectedItems: Collection of currently selected item IDs (optional array in the generated initializer). In single-selection mode only the first element is considered; in multiple-selection mode all elements are used.

Selection Mode

Controlled externally via environment:

.environment(\.hierarchyItemSelectionMode, .none)     // selection disabled
.environment(\.hierarchyItemSelectionMode, .single)   // single selection
.environment(\.hierarchyItemSelectionMode, .multiple) // multi selection

Selection affordances (selection buttons) are only visible while EditMode is .active and the selection mode is not .none.

Usage

1. Simple Initialization (generated initializer)

@State var activeChildItem: String?
@State var selectedItems: [String]? = []
@State var isEditing = true
@State var selectionMode = HierarchyItemSelectionMode.single

let dataSource = HierarchySimpleDataSource()

HierarchyView(
    dataSource: dataSource,
    hierarchyItem: { id in
        title: { Text(id) },
        hierarchyIndicator: {
            let childrenCount = dataSource.numberOfChildren(for: id)
            HierarchyIndicator(
                title: AttributedString(String("Indicator \(id)")),
                isMultiline: self.isMultiline,
                isSelected: activeChildItem == id,
                isClickable: childrenCount > 0
         ){
            id
         }
   },
   activeChildItem: self.$activeChildItem,
   selectedItems: self.$selectedItems
)
.environment(\.editMode, .constant(isEditing ? EditMode.active : EditMode.inactive))
.environment(\.hierarchyItemSelectionMode, selectionMode)

2. Single Selection Convenience (see public API extension)

@State private var activeChild: String? = nil
@State private var selected: String? = nil
HierarchyView.singleSelection(
    dataSource: dataSource,
    hierarchyItem: { id in Text(id) },
    activeChildItem: $activeChild,
    selectedItem: $selected
)
.environment(\.hierarchyItemSelectionMode, .single)

3. Multi Selection With Set (see public API extension)

@State private var activeChild: String? = nil
@State private var selectedSet: Set<String> = []
HierarchyView.multiSelection(
    dataSource: dataSource,
    hierarchyItem: { id in Text(id) },
    activeChildItem: $activeChild,
    selectedItems: $selectedSet
)
.environment(\.hierarchyItemSelectionMode, .multiple)

Styling

Apply or compose styles using:

HierarchyView(...)
.hierarchyViewStyle(MyCustomHierarchyStyle())

Custom styles implement HierarchyViewStyle and can be layered; the environment maintains an internal style stack.

See Also

HierarchyViewDataSource, HierarchyItemView, HierarchyHeader, HierarchyIndicator, HierarchyViewStyle.