BLACK FRIDAY: Save 50% 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)
}

}

3      

  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)
}

4      

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)
}

}

3      

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

2      

Save 50% in my WWDC sale.

SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.