# List

## Overview

## Subjects

### Form vs List

* Form is for User Input
* List is for Presentation of data

### Edge to Edge Color

{% code title="Edge to edge color" %}

```swift
.listStyle(.plain)
```

{% endcode %}

### List with row number

```swift
Let list =["Car", "Bike", "Plane", "Boat"]
for(index, element) inlist.enumerated() {    
print(index, ":", element)}

// print: 0 : Car1 : Bike2 : Plane3 : Boat
```

### List with Sections

```swift
VStack {
    List {
        ForEach(countries, id: \.self) { country in
            Section(country.wrappedFullName) {
                ForEach(country.candyArray, id: \.self) { candy in
                    Text(candy.wrappedName)
                }
            }
        }
    }
```

![](https://5317963-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F598GXhEFvy2PX7VpZjCw%2Fuploads%2FfHVOoFj86KLrq16UgloV%2FIMG_0347.jpeg?alt=media\&token=9d856afb-95dc-4732-ad6c-3858deb1de17)

### Sorted List

```swift
// FIRST METHOD
struct User: Identifiable {
    let id = UUID()
    let firstName: String
    let lastName: String
}

struct ContentView: View {
let users = [
    User(firstName: "Arnold", lastName: "Rimmer"),
    User(firstName: "Kristine", lastName: "Kochanski"),
    User(firstName: "David", lastName: "Lister"),
].sorted {
    $0.lastName < $1.lastName
}

//.............................

// SECOND METHOD

struct User: Identifiable {
    let id = UUID()
    let firstName: String
    let lastName: String
    
    static func <(lhs: User, rhs: User) -> Bool {
        lhs.lastName < rhs.lastName
    }
    
}

struct ContentView: View {
let users = [
    User(firstName: "Arnold", lastName: "Rimmer"),
    User(firstName: "Kristine", lastName: "Kochanski"),
    User(firstName: "David", lastName: "Lister"),
].sorted()
}
```

The result is the same on both methods. The benefit is, in case of using this list everywhere with same sorting method, we can change the sorting method in the struct and it changes everywhere we use the list.

<figure><img src="https://5317963-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F598GXhEFvy2PX7VpZjCw%2Fuploads%2FdvNBTXvADRdqYcBWfVU2%2F6F0B0E51-B8E3-4263-BCE1-037ADFB26DA6_1_201_a.jpeg?alt=media&#x26;token=5cbee065-ec2f-43b1-9d67-e12959eadfe3" alt=""><figcaption><p>Result</p></figcaption></figure>

{% embed url="<https://youtu.be/SPML9uatLf8>" %}
[Hacking with Swift / 100 Days / Day-68 - Adding conformance to Comparable for custom types](https://www.hackingwithswift.com/books/ios-swiftui/adding-conformance-to-comparable-for-custom-types)
{% endembed %}

### Swipe Actions

{% code title="Double Sided Swipe Action" %}

```swift
List {
    Text("Taylor Swift")
        .swipeActions {
            Button(role: .destructive) {
                print("Hi")
            } label: {
                Label("Delete", systemImage: "minus.circle")
            }
        }
        .swipeActions(edge: .leading) {
            Button {
                print("Hi")
            } label: {
                Label("Pin", systemImage: "pin")
            }
            .tint(.orange)
        }
}
```

{% endcode %}

{% embed url="<https://youtu.be/XEaoFCOll94>" %}
[Hacking with Swift / 100 Days / Day-81 - Adding custom row swipe actions to a List](https://www.hackingwithswift.com/books/ios-swiftui/adding-custom-row-swipe-actions-to-a-list)
{% endembed %}

#### Other sources

* [Peter Friese / The Ultimate Guide to SwiftUI List Views - Part 4 / Swipe Actions in SwiftUI 3](https://peterfriese.dev/posts/swiftui-listview-part4/)

### Adding options with Swipe Actions

{% embed url="<https://youtu.be/oZzmHrFfWhg>" %}
[Hacking with Swift / 100 Days / Day-83 - Adding options with swipe actions](https://www.hackingwithswift.com/books/ios-swiftui/adding-options-with-swipe-actions)
{% endembed %}

### Sorting the List

{% embed url="<https://www.youtube.com/watch?v=hMZdRduyA_4>" %}

Define the alternatives

```swift
enum SortOption {
    case alt1, alt1Reversed, alt2, alt2Reversed
    }
```

Convert enum type to variable and Publish it

```swift
@Published var sortOption: SortOption = .alt2
```

## Sources

### Videos

{% embed url="<https://youtu.be/U1NxVE02Og8>" %}

### Articles / Documents

* [Peter Friese / The Ultimate Guide to SwiftUI List Views - Part 2 / Building Dynamic Lists in SwiftUI](https://peterfriese.dev/posts/swiftui-listview-part2/)
* [Peter Friese / The Ultimate Guide to SwiftUI List Views - Part 2 / Building Static Lists in SwiftUI](https://peterfriese.dev/posts/swiftui-listview-part1/)
* [Peter Friese / The Ultimate Guide to SwiftUI List Views - Part 3 / Styling List Views](https://peterfriese.dev/posts/swiftui-listview-part3/)
* <https://stackoverflow.com/questions/24028421/how-to-iterate-a-loop-with-index-and-element-in-swift/24028458#24028458>
