Skip to content

Instantly share code, notes, and snippets.

@Kazunari-h
Created December 3, 2021 05:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kazunari-h/30ee173d86e95917c2f17533eb446962 to your computer and use it in GitHub Desktop.
Save Kazunari-h/30ee173d86e95917c2f17533eb446962 to your computer and use it in GitHub Desktop.
IP3S
//
// Cell.swift
// marubatsu
//
// Created by 29MCGD016 on 2021/11/26.
//
import SwiftUI
struct Cell: View {
var boxState: Bool?
var action: () -> ()
var body: some View {
Button(action: {
self.action()
}, label: {
if let _boxState = boxState {
Image(systemName: _boxState ? "circle" : "xmark")
} else {
// nil
Text("s")
.foregroundColor(.white)
}
})
.padding(20)
.frame(width: 100, height: 100)
.background(Color.white)
.foregroundColor(boxState ?? true ? .red : .blue)
}
}
struct Cell_Previews: PreviewProvider {
static var previews: some View {
Cell {}
}
}
//
// ContentView.swift
// marubatsu
//
// Created by 29MCGD016 on 2021/11/05.
//
import SwiftUI
struct ContentView: View {
@State var boxList: [[Bool?]] = [
[nil, nil, nil],
[nil, nil, nil],
[nil, nil, nil]
]
var body: some View {
VStack(spacing: 1) {
ForEach(Array(zip(boxList.indices, boxList)), id: \.0) { (v, row) in // [Bool?]
HStack(spacing: 1) {
ForEach(Array(zip(row.indices, row)), id: \.0) { (h, boxState) in
Cell(boxState: boxState) {
boxList[v][h] = !(boxState ?? false)
}
}
}
}
}
.background(Color.black)
Button(action: {
// if box1 == nil {
// box1 = true
// } else {
// box1?.toggle()
// }
}, label: {
// if let _box1 = box1 {
// Image(systemName: _box1 ? "circle" : "xmark")
// } else {
// // nil
// Text("a")
// }
})
.background(Color.red)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment