Reputation: 13
I am a swift and iOS beginner and I am working on an app that will mostly feature tableviews of editable data.
The example from Apple's tableview programming guide is a lot like my app in structure. Although my app will be using people and teams, to make it clear I will refer to Apple's example. Using Figure 3-1 as a reference I would like to be able to add, edit and delete Regions, add, edit and delete trails and to edit the properties of the trails. I have succeeded with the trails array and trails properties but now I find I need to organize the trails into regions as well. Should the regions array be an array of array of trails?
I apologize if the question is too broad but I don't know how else to ask it at this stage. I have tried various approaches but one way or the other the data doesn't get edited correctly.
I am just looking for hint of which way to proceed since I have spent many days wrestling with this.
Thank you.
Upvotes: 0
Views: 66
Reputation: 3538
You can have two structs of region and trail. something like below:
struct Trail {
var location: String
var distance: Double
var difficulty: String
}
struct Region {
var name:String
var trails: [Trail] // this will hold array of trails
}
var regionsArray: [Region] = []
// let create trails first
let trails = [
Trail(location: "ABC", distance: 1.0, difficulty: "moderate"),
Trail(location: "DEF", distance: 1.0, difficulty: "easy")
]
// then create region with its trails
let eastBay = Region(name: "East Bay", trails: trails)
// append and print array of regions
regionsArray.append(eastBay)
print(regionsArray.append)
Take note that This is just one way to add region with trails. You can have in loops or assign in array
Upvotes: 2