First let me show the sample code:
checkboxlist:
struct CheckListView: View {
var body: some View {
List(checkListData){ item in
CheckView(isChecked: item.isChecked, title: item.title)
}
.font(.title)
}
}
single checkbox:
struct CheckView: View {
@State var isChecked:Bool = false
var title:String
func toggle(){isChecked = !isChecked}
var body: some View {
HStack{
Button(action: toggle) {
Image(systemName: isChecked ? "checkmark.square" : "square")
}
Text(title)
}
}
}
the datas:
let checkListData = [
CheckListItem(id:0,title: "Neopolitan"),
CheckListItem(id:1,title: "New York"),
CheckListItem(id:2,title: "Hawaiian"),
.....
and the CheckListItem:
struct CheckListItem:Identifiable{
var id:Int
var isChecked: Bool = false
var title: String
}
I guess lots of guys already had this issue, suppose you have lots of checkboxes in you checkbox list which is enough to scroll, you check the first 1 or 2 or a few at the foremost of the list, then you scroll to the bottom, and scroll back again, those checked checkbox are turned back!
But, at this morning I suddenly found that if you use ForEach inside the List, then everything works fine:
List {
ForEach(checkListData, id: \.id){ item in
CheckView(isChecked: item.isChecked, title: item.title)
}
}.font(.title)
Can anyone explain why that weird issue happen and why this way is working fine? Thanks.
