'Listing items in a sequence
can someone please help me with this.This is an example of creating List. The items are numbered as 1, 2, 3 ... The issue I am facing is, when I delete any items from the list, the numbering doesn't get re-arranged. For example if there are 7 items in the list 1, 2, 3, 4, 5, 6, 7 and if I delete any item for example: number 3 and 6, the list appears as 1, 2, 4, 5, 7. How can I re-arrange this list after deleting so that the items are numbered serially such as 1, 2, 3, 4, 5 instead of 1, 2, 4, 5, 7.
import SwiftUI
struct Calculation: View {
@State var load1 = Float()
@State var load2 = Float()
@State var gp : Float = 0
@State var rate: Float = 0
@ObservedObject var taskStore = TaskStore()
@State private var count: Double = 1
func addNewToDo() {
taskStore.tasks.append(Task(id: String(taskStore.tasks.count + 1), toDoItem: " \(count) Earning: = \(rate.description)" ))
}
func stepcount() {
count += 1
}
var body: some View {
NavigationView {
VStack {
List {
Section(header:Text("load 2"))
{
TextField("Enter value of load 1", value: $load1, format: .number)
TextField("Enter value of load 1", value: $load2, format: .number)
}
HStack {
Button(String(format: "Add Load"), action:
{
print("pay for the shift is ")
print(Rocky(mypay: rate))
gp += rate
})
Button(action: {addNewToDo();stepcount(); Rocky(mypay: rate) }, label: {
Text(" ")
})
}
ForEach(self.taskStore.tasks) {
task in
Text(task.toDoItem)
}.onMove(perform : self.move)
.onDelete(perform : self.delete) //For each
}.navigationBarTitle("SHIFTS")
.navigationBarItems(trailing: EditButton()) //List
Text("Gross Pay = $\(gp) ")
}.onAppear()
} }
func Rocky(mypay: Float)
{ rate = load1 + load2
print("Sus \(gp)")
}
func move(from source : IndexSet, to destination : Int)
{
taskStore.tasks.move(fromOffsets: source, toOffset: destination)
}
func delete(at offsets : IndexSet) {
taskStore.tasks.remove(atOffsets: offsets)
gp -= rate
}
}
struct BlueTwoView_Previews: PreviewProvider {
static var previews: some View {
Calculation()
}
}
Here is another file:
import Foundation
import SwiftUI
import Combine
class UserSettings: ObservableObject {
@Published var gp : Float = 0
@Published var Homevariable = Bool()
}
struct Task : Identifiable {
var id = String()
var toDoItem = String()
var amount : Float = 0
}
class TaskStore : ObservableObject {
@Published var tasks = [Task]()
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
