Skip to content

Tap to check the ListView

   

Here's a sample of tapping to check the ListView. Tap on it to change the state and draw ✔︎.

CheckList

import SwiftUI
struct Fruit: Identifiable, Equatable {
let id: Int
var check: Bool
var name: String
}
struct ListRow: View {
let check: Bool
let name: String
var body: some View {
HStack {
if check {
Text("✔︎")
} else {
Text(" ")
}
Text(name)
Spacer()
}
}
}
struct ContentView: View {
@State var fruits: [Fruit] = [
Fruit(id: 0, check: true, name: "Apple"),
Fruit(id: 1, check: true, name: "Banana"),
Fruit(id: 2, check: true, name: "Orange"),
Fruit(id: 3, check: true, name: "Grape"),
Fruit(id: 4, check: true, name: "WaterMelon"),
]
var body: some View {
VStack {
Divider()
List (fruits) { fruit in
Button(action: {
guard let index = self.fruits.firstIndex(of: fruit) else {
return
}
self.fruits[index].check.toggle()
}) {
ListRow(check: fruit.check, name: fruit.name)
}
}
.edgesIgnoringSafeArea([.bottom])
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

  1. Create a sequential numbered array in Swift
  2. Aggregating CSV with Swift
  3. Make the background of UIView a grid or a dot.
  4. Using ReplayKit to record a screen
  5. Aligning Views like UICollectionView in SwiftUI
  6. Displaying a Firestore image in SwiftUI
  7. Detecting changes to the SwiftUI Toggle