Last active
June 27, 2024 15:47
-
-
Save devs-rootstrap/f30248ee73689c540a256417e134b399 to your computer and use it in GitHub Desktop.
SwiftUI List
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct Pet: Identifiable, Hashable { | |
let id = UUID() | |
let name: String | |
} | |
struct PetCustomRow: View { | |
let pet: Pet | |
var body: some View { | |
HStack { | |
Text(pet.name) | |
.font(.headline) | |
.foregroundColor(.primary) | |
Spacer() | |
Image(systemName: "pawprint") | |
.imageScale(.large) | |
.foregroundColor(.secondary) | |
} | |
.listRowBackground(Color.gray.opacity(0.1)) | |
.listRowInsets(EdgeInsets(top: 10, leading: 10, bottom: 10, trailing: 10)) | |
} | |
} | |
struct ContentView: View { | |
@State private var pets = [ | |
Pet(name: "Buddy"), | |
Pet(name: "Whiskers"), | |
Pet(name: "Fido"), | |
Pet(name: "Luna"), | |
Pet(name: "Mittens") | |
] | |
var body: some View { | |
NavigationView { | |
List { | |
ForEach(pets, id: \.self) { pet in | |
PetCustomRow(pet: pet) | |
} | |
.onMove { source, destination in | |
pets.move(fromOffsets: source, toOffset: destination) | |
} | |
.onDelete { offset in | |
pets.remove(atOffsets: offset) | |
} | |
} | |
.listStyle(InsetGroupedListStyle()) | |
.navigationBarTitle("Pets") | |
.navigationBarItems(trailing: EditButton()) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct Pet: Identifiable, Hashable { | |
let id = UUID() | |
let name: String | |
} | |
struct PetCustomRow: View { | |
let pet: Pet | |
var body: some View { | |
HStack { | |
Text(pet.name) | |
.font(.headline) | |
.foregroundColor(.primary) | |
Spacer() | |
Image(systemName: "pawprint") | |
.imageScale(.large) | |
.foregroundColor(.secondary) | |
} | |
.listRowBackground(Color.gray.opacity(0.1)) | |
.listRowInsets(EdgeInsets(top: 10, leading: 10, bottom: 10, trailing: 10)) | |
} | |
} | |
struct ContentView: View { | |
@State private var pets = [ | |
Pet(name: "Buddy"), | |
Pet(name: "Whiskers"), | |
Pet(name: "Fido"), | |
Pet(name: "Luna"), | |
Pet(name: "Mittens") | |
] | |
var body: some View { | |
NavigationView { | |
List { | |
ForEach(pets, id: \.self) { pet in | |
PetCustomRow(pet: pet) | |
} | |
.onMove { source, destination in | |
pets.move(fromOffsets: source, toOffset: destination) | |
} | |
} | |
.listStyle(InsetGroupedListStyle()) | |
.navigationBarTitle("Pets") | |
.navigationBarItems(trailing: EditButton()) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct Pet: Identifiable { | |
let id = UUID() | |
let name: String | |
} | |
struct ContentView: View { | |
let pets = [ | |
Pet(name: "Buddy"), | |
Pet(name: "Whiskers"), | |
Pet(name: "Fido"), | |
Pet(name: "Luna"), | |
Pet(name: "Mittens") | |
] | |
var body: some View { | |
List(pets) { pet in | |
PetCustomRow(pet: pet) | |
} | |
.listStyle(InsetGroupedListStyle()) | |
.navigationBarTitle("Pets") | |
} | |
} | |
struct PetCustomRow: View { | |
let pet: Pet | |
var body: some View { | |
HStack { | |
Text(pet.name) | |
.font(.headline) | |
.foregroundColor(.primary) | |
Spacer() | |
Image(systemName: "pawprint") | |
.imageScale(.large) | |
.foregroundColor(.secondary) | |
} | |
.listRowBackground(Color.gray.opacity(0.1)) | |
.listRowInsets(EdgeInsets(top: 10, leading: 10, bottom: 10, trailing: 10)) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct Pet: Identifiable { | |
let id = UUID() | |
let name: String | |
} | |
struct ContentView: View { | |
let pets = [ | |
Pet(name: "Buddy"), | |
Pet(name: "Whiskers"), | |
Pet(name: "Fido"), | |
Pet(name: "Luna"), | |
Pet(name: "Mittens") | |
] | |
var body: some View { | |
List(pets) { pet in | |
Text(pet.name) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct Pet: Identifiable { | |
let id = UUID() | |
let name: String | |
} | |
struct ContentView: View { | |
let pets = [ | |
Pet(name: "Buddy"), | |
Pet(name: "Whiskers"), | |
Pet(name: "Fido"), | |
Pet(name: "Luna"), | |
Pet(name: "Mittens") | |
] | |
var body: some View { | |
List(pets) { pet in | |
HStack { | |
Text(pet.name) | |
.font(.headline) | |
.foregroundColor(.primary) | |
Spacer() | |
Image(systemName: "pawprint") | |
.imageScale(.large) | |
.foregroundColor(.secondary) | |
} | |
.listRowBackground(Color.gray.opacity(0.1)) | |
.listRowInsets(EdgeInsets(top: 10, leading: 10, bottom: 10, trailing: 10)) | |
} | |
.listStyle(InsetGroupedListStyle()) | |
.navigationBarTitle("Pets") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment