HELP! I can't figure this out!
To help me learn SwiftUI I am making a simple grocery list app using a list view. There are buttons that turn GREEN or RED when tapped for each item on the list.
The code below works great until I add too many items to my list array. If the list gets longer than the screen size and you have to scroll then weird things start happening. (like the button won't turn green or an item on the next page turns green instead)
I tested with different simulators and on an iPhone SE I can get 14 items on the list before it starts messing up. On an iPhone 11 Pro Max, I can get 22 items on the list before it starts messing up. It has something to do with the length of the screen size and if scrolling is required.
Here is the code:
import SwiftUI
struct CheckboxFieldView : View {
@State var checkState:Bool = false ;
var body: some View {
Button(action:
{
self.checkState = !self.checkState
}) {
Rectangle()
.fill(self.checkState ? Color.green : Color.red)
.frame(width:20, height:20, alignment: .center)
.cornerRadius(5)
}
.foregroundColor(Color.white)
}
}
struct ContentView: View {
// let items = (1...100).map { number in "Item \(number)" }
let items = ["Bread", "Milk", "Cheese", "Granola", "Nuts", "Cereal", "Rosemary", "Tomato Sauce", "Bean with Bacon Soup", "Tea", "Chocolate Milk", "Frozen Blueberries", "Frozen Mixed Berries", "Frozen Strawberries", "Grapes"]
var body: some View {
NavigationView {
List(items, id: \.self) { item in
HStack{
CheckboxFieldView()
Text(item)
self.padding()
Text ("Location")
}
}.navigationBarTitle("Grocery List", displayMode: .inline)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}