BLACK FRIDAY SALE: Save big on all my Swift books and bundles! >>

SOLVED: Habbit-Tracking.App, Challenge 7-9, how to implement .onDelete()

Forums > SwiftUI

I've just finished theHabbit-Tracking.App

Now I wolud like to implment 'removeRows'. I know I have to do this with a ForEach .onDelete and a function removeRows. It was not possible to bring it to run so far. Can help me somebody waht I'm doing wrong?

Here is the code from contenView.

  • What do I have to call in 'ForEach' for Reference and id?
  • What ist the correct reference in the function removeRows?

The rest of the app is running fine.

import SwiftUI

struct ContentView: View {
  @StateObject var data = Activities()
  @State private var addingNewActivity = false

  var body: some View {
    NavigationView {
        List(data.activities) { activity in
            NavigationLink {
                ActivityView(data: data, activity: activity)
            } label: {

            // What it the correct Reference in ForEach?
                ForEach(data, id: .id ) {
                    HStack {
                        Text(activity.title)
                        Spacer()

                        Text(String(activity.completionCount))
                            .font(.caption.weight(.black))
                            .padding(5)
                            .frame(minWidth: 50)
                            .background(color(for: activity))
                            .clipShape(Capsule())
                    }
                }
                .onDelete(perform: removeRows)
            }
        }
        .navigationTitle("Habito")
        .toolbar {
            HStack {
                EditButton()

                Button {
                    addingNewActivity.toggle()
                } label: {
                    Label("Add new activity", systemImage: "plus")
                }
            }
        }
        .sheet(isPresented: $addingNewActivity) {
            AddActivityView(data: data)
        }
    }
}
func color(for activity: Activity) -> Color {
    if activity.completionCount < 3 {
        return .red
    } else if activity.completionCount < 10 {
        return .orange
    } else if activity.completionCount < 20 {
        return .green
    } else if activity.completionCount < 50 {
        return .blue
    } else {
        return .indigo
    }
}

func removeRows(at offsets: IndexSet) { 
// What do I have to call here?
        data.remove(atOffsets: offsets)
}

}

1      

  1. You don't need to pass anything to List for it to loop through
  2. The ForEach should go around the NavigationLink rather than inside it
  3. ForEach should be looping through data.activities, not just data
  4. The ForEach needs a parameter name for the trailing closure
  5. In your removeRows function, call remove(at:) on data.activities
List {
    ForEach(data.activities) { activity in
        NavigationLink {
            ActivityView(data: data, activity: activity)
        } label: {
            HStack {
                Text(activity.title)
                Spacer()
                Text(String(activity.completionCount))
                    .font(.caption.weight(.black))
                    .padding(5)
                    .frame(minWidth: 50)
                    .background(color(for: activity))
                    .clipShape(Capsule())
            }
        }
    }
    .onDelete(perform: removeRows)
}
func removeRows(at offsets: IndexSet) {
    data.activities.remove(atOffsets: offsets)
}

2      

Hello roosterboy Tank a lot you for your advice! I had to put the List into the NavigationView to have the Toolbar in place. Now 'onDelete()' works like aspected.

Here the code of CotentView with 'onDelete() in place.

import SwiftUI

struct ContentView: View {
    @StateObject var data = Activities()
    @State private var addingNewActivity = false

  var body: some View {
    NavigationView {
        List {
            ForEach(data.activities) { activity in
                NavigationLink {
                    ActivityView(data: data, activity: activity)
                } label: {
                    HStack {
                        Text(activity.title)
                        Spacer()
                        Text(String(activity.completionCount))
                            .font(.caption.weight(.black))
                            .padding(5)
                            .frame(minWidth: 50)
                            .background(color(for: activity))
                            .clipShape(Capsule())
                    }
                }
            }
            .onDelete(perform: removeRows)
        }
        .navigationTitle("Habito")
        .toolbar {
            HStack {
                EditButton()

                Button {
                    addingNewActivity.toggle()
                } label: {
                    Label("Add new activity", systemImage: "plus")
                }
            }
        }
        .sheet(isPresented: $addingNewActivity) {
            AddActivityView(data: data)
        }
    }
}

func color(for activity: Activity) -> Color {
    if activity.completionCount < 3 {
        return .red
    } else if activity.completionCount < 10 {
        return .orange
    } else if activity.completionCount < 20 {
        return .green
    } else if activity.completionCount < 50 {
        return .blue
    } else {
        return .indigo
    }
}

func removeRows(at offsets: IndexSet) {
    data.activities.remove(atOffsets: offsets)
}

}

1      

Thank you for the solution..so the answer if that without a Foreach is impossibile to remove the row?

   

Hacking with Swift is sponsored by RevenueCat

SPONSORED In-app subscriptions are a pain to implement, hard to test, and full of edge cases. RevenueCat makes it straightforward and reliable so you can get back to building your app. Oh, and it's free if your app makes less than $10k/mo.

Learn more

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.