Created
October 9, 2020 05:53
-
-
Save StewartLynch/7cc3cb03c12614583dcebc5abed81015 to your computer and use it in GitHub Desktop.
App crashes when I try to delete tab
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Item: Identifiable { | |
let id = UUID() | |
var index: Int | |
static var mockData:[Item] { | |
return (0...2).map{Item(index: $0)} | |
} | |
} | |
struct ContentView: View { | |
@State var items = Item.mockData | |
var body: some View { | |
NavigationView { | |
TabView() { | |
ForEach(items) { item in | |
VStack { | |
Text("Item \(item.index)") | |
Button("Delete") { | |
items.remove(at: indexOf(item: item)) | |
} | |
} | |
} | |
} | |
.navigationBarItems(trailing: Button("Add") { | |
items.append(Item(index: items.count)) | |
}) | |
.tabViewStyle(PageTabViewStyle()) | |
.indexViewStyle(PageIndexViewStyle(backgroundDisplayMode: .always)) | |
.navigationTitle("Test Delete") | |
} | |
} | |
func indexOf(item:Item) -> Int { | |
items.firstIndex(where: {$0.id == item.id})! | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment