Post

Replies

Boosts

Views

Activity

Reply to Migrating selection-based navigation inside ForEach
Finally figured it out myself, if you see any possible improvement of my code I am happy to learn :-) struct ItemList: View {     @StateObject private var itemStore = ItemStore()     @State private var newItem = false     @State private var indexItem: Int?     var body: some View {         NavigationStack() {             List {                 let itemStoreZip = zip(itemStore.allItems.indices, itemStore.allItems)                 ForEach(Array(itemStoreZip), id: \.0) {i, item in                     let itemBind = $itemStore.allItems[i]                     NavigationLink {                         ItemDetail(item: itemBind)                     } label: {                         ItemRow(item: item)                     }                                      }                 .onDelete { indexSet in                     itemStore.removeItem(indexSet: indexSet)                 }                 .onMove { indices, newOffset in                     itemStore.moveItem(source: indices, to: newOffset)                 }             }             .navigationDestination(isPresented: $newItem) {                 if let indexItem {                     ItemDetail(item: $itemStore.allItems[indexItem])                 }             }             .navigationTitle("Homepwner")             .toolbar {                 ToolbarItem(placement: ToolbarItemPlacement.navigationBarTrailing) {                     EditButton()                 }                 ToolbarItem(placement: ToolbarItemPlacement.navigationBarLeading) {                     Button {                         (indexItem,_) = itemStore.newItem()                         newItem = true                     } label: {                         Image(systemName: "plus")                     }                 }             }         }     } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’23
Reply to Migrating selection-based navigation inside ForEach
Finally figured it out myself, if you see any possible improvement of my code I am happy to learn :-) struct ItemList: View {     @StateObject private var itemStore = ItemStore()     @State private var newItem = false     @State private var indexItem: Int?     var body: some View {         NavigationStack() {             List {                 let itemStoreZip = zip(itemStore.allItems.indices, itemStore.allItems)                 ForEach(Array(itemStoreZip), id: \.0) {i, item in                     let itemBind = $itemStore.allItems[i]                     NavigationLink {                         ItemDetail(item: itemBind)                     } label: {                         ItemRow(item: item)                     }                                      }                 .onDelete { indexSet in                     itemStore.removeItem(indexSet: indexSet)                 }                 .onMove { indices, newOffset in                     itemStore.moveItem(source: indices, to: newOffset)                 }             }             .navigationDestination(isPresented: $newItem) {                 if let indexItem {                     ItemDetail(item: $itemStore.allItems[indexItem])                 }             }             .navigationTitle("Homepwner")             .toolbar {                 ToolbarItem(placement: ToolbarItemPlacement.navigationBarTrailing) {                     EditButton()                 }                 ToolbarItem(placement: ToolbarItemPlacement.navigationBarLeading) {                     Button {                         (indexItem,_) = itemStore.newItem()                         newItem = true                     } label: {                         Image(systemName: "plus")                     }                 }             }         }     } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jan ’23