I'm working with a project in SwiftUI. I want to create something like,
The code I'm using currently:
TaskListView
struct TaskListView: View {
var tasks: [Task] = Task.all()
var body: some View {
List {
ForEach(self.tasks) {task in
TaskView(task: task)
}
}
}
}
TaskView
struct TaskView: View {
@ObservedObject var task: Task
var body: some View {
VStack {
Text(task.name)
.font(.custom("Avenir Next Regular", size: 14))
//Here.................
if !task.subtasks.isEmpty {
Section {
ForEach(task.subtasks) {subtask in
TDTaskView(task: subtask)
}
}.padding(.leading)
}
}
}
}
Task model:
class Task: Identifiable, ObservableObject {
var id: UUID = UUID()
var name: String
@Published var isCompleted: Bool = false
var subtasks = [Task]()
init(name: String, isCompleted: Bool = false, subtasks: [Task] = [Task]()) {
self.name = name
self.isCompleted = isCompleted
self.subtasks = subtasks
}
}
The way I'm trying to implement nested List is now working properly on selection. Am I implementing it wrongly?
