Hello everybody.
I have an array of students which the app should divide into 5 groups wich should be shown in different sections of the List.
To do this I have created the List:
List{
Section("Group 1") {
ForEach(splitArray(schueler)[0], id: \.self) {SchuelerName in Text(SchuelerName)}
.onDelete(perform: delete)
}
Section("Group 2"){
(ForEach(splitArray(schueler)[1], id: \.self) {SchuelerName in Text(SchuelerName)}
.onDelete(perform: delete))
}
Section("Group 3"){
(ForEach(splitArray(schueler)[2], id: \.self) {SchuelerName in Text(SchuelerName)}
.onDelete(perform: delete))
}
Section("Group 4"){
(ForEach(splitArray(schueler)[3], id: \.self) {SchuelerName in Text(SchuelerName)}
.onDelete(perform: delete))
}
Section("Group 5"){
(ForEach(splitArray(schueler)[4], id: \.self) {SchuelerName in Text(SchuelerName)}
.onDelete(perform: delete))
}
And a function that defines my groups by splitting my array:
func splitArray<T>(_ array: [T]) -> [[T]] { let count = array.count let chunkSize = count / 5 var remainder = count % 5 var startIndex = 0 var result: [[T]] = []
for _ in 1...5 {
let chunkCount = chunkSize + (remainder > 0 ? 1 : 0)
let endIndex = startIndex + chunkCount
let chunk = Array(array[startIndex..<endIndex])
result.append(chunk)
startIndex = endIndex
remainder -= 1
}
return result
}
the delete function I have designed a always:
func delete (indexSet: IndexSet) {
schueler.remove(atOffsets: indexSet)
my problem is that when I now delete a Student from the second section, he just goes into the first one and another one gets deleted.
Does anyone know why this code be the case and how to fix it? Thanks a lot in advance