Skip to content

Instantly share code, notes, and snippets.

@devs-rootstrap
Last active June 27, 2024 15:47
Show Gist options
  • Save devs-rootstrap/f30248ee73689c540a256417e134b399 to your computer and use it in GitHub Desktop.
Save devs-rootstrap/f30248ee73689c540a256417e134b399 to your computer and use it in GitHub Desktop.
SwiftUI List
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())
}
}
}
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())
}
}
}
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))
}
}
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)
}
}
}
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