GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

SOLVED: Add sections to customer List

Forums > SwiftUI

Greeting all

I've created a customer list but I'm struggling to split it into sections based on my model. Could anyone point me in the right direction on how to solve.

Kind Regards

View

struct simoView: View {
    var body: some View {
      let simos: [Simo] = normalSimList.fullPriceSim
      List(simos, id: \.id) { simo in
        Section {

            VStack(alignment: .leading, spacing: 5) {
              Text(simo.title)
                .fontWeight(.semibold)
                .lineLimit(2)
                .minimumScaleFactor(0.5)
              HStack {
                Text(simo.price)
                    .font(.subheadline)
                    .foregroundStyle(.secondary)
                Spacer()
                Text(simo.sku)
                    .font(.subheadline)
                    .foregroundStyle(.secondary)
                Spacer()
                Text(simo.rev)
                    .font(.subheadline)
                    .foregroundStyle(.secondary)
              }
            }
        }
      }
    }
}

Model:

struct Simo: Identifiable {
    let id = UUID()
    let imageName = "simcard.fill"
    let title: String
    let price: String
    let sku: String
    let rev: String
}

struct addLineSimList {
    static let addLineSim = [
    Simo(title: "100GB", price: "£10.00", sku: "120753", rev: "£52.80"),
    Simo(title: "100GB Roam 51", price: "£17.00", sku: "...", rev: "..."),
    Simo(title: "150GB", price: "£13.00", sku: "120754", rev: "£68.63"),
    Simo(title: "Unlimited Max", price: "£20.00", sku: "120755", rev: "£105.60"),
    ]
}

struct normalSimList {
    static let fullPriceSim = [
      Simo(title: "50GB", price: "£21.00", sku: "121079", rev: "£140.80"),
      Simo(title: "80GB Ent", price: "£29.00", sku: "121089", rev: "£158.26"),
      Simo(title: "150GB", price: "£25.00", sku: "121081", rev: "£161.92"),
      Simo(title: "150GB Roam 51", price: "£32.00", sku: "120420", rev: "£168.96"),
      Simo(title: "Unlimited Max", price: "£33.00", sku: "120169", rev: "£203.32"),
      Simo(title: "Unlimited Max Roam 51", price: "£40.00", sku: "120385", rev: "£281.60"),
      Simo(title: "Unlimited Max Roam 83", price: "£43.00", sku: "120395", rev: "£295.68")
    ]
}

   

Because the struct Simo has an identifier, you named id, you do not need to indicate id in the List command.

//          👇🏼 Not necessary
List(simos, id: \.id) { simo in
        Section {

            VStack(alignment: .leading, spacing: 5) {
              Text(simo.title)
                .fontWeight(.semibold)
// ..... snip .............

1      

Hmm.. I'd review your structs, I don't think they are really giving you value. Since they are all the same object types Simo, just under a different category, I'd suggest you just add a category property and then group by that... That said, with your current code:

 List {
   Section("Full Price Sims") {
       ForEach(normalSimList.fullPriceSim) { fullPriceSim in
           Text(fullPriceSim.title)
           ....
        }
     }

      Section("Add. Line Sims") {
       ForEach(addLineSimList.addLineSim) { addLineSim in
           Text(addLineSim.title)
           ....
        }
     }
 }

1      

Hacking with Swift is sponsored by Essential Developer.

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until February 9th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.