Home

lovelejess

17 Nov 2020

swiftui - lazy stacks versus lists

Notes from Ray Wenderlich’s Swift UI - Layout Interfaces

Hacking with Swift has a great article as well

Lists

  • Similar to UITableView and UITableViewCell in UIKit
  • A specialized version of lazy v stacks
  • They are more common to use
  • They have built in functionality, such as swiping to delete, reordering, and styling (similar to table views)
  • Lazy loads the data, but the data is loaded up front
struct ContentView: View {
    var body: some View {
        List {
            let formatter: DateFormatter = {
                let formatter = DateFormatter()
                formatter.timeStyle = .medium
                return formatter
            } ()
            ForEach(1...10000, id: \.self) { item in
                Text(
                    formatter.string(from: Date())
                )
            }
        }
    }
}

Lazy Stacks

  • Lazy loads the data, but the data is loaded just in time (aka when it comes into view)
  • Use Lazy Stacks when you notice a performance issue
struct ContentView: View {
    var body: some View {
        ScrollView {
            LazyVStack {
                let formatter: DateFormatter = {
                    let formatter = DateFormatter()
                    formatter.timeStyle = .medium
                    return formatter
                } ()
                ForEach(1...10000, id: \.self){ item in
                    Text(
                        formatter.string(from: Date())
                    )
                }
            }
        }
    }
}

Til next time,
lovelejess at 21:57

scribble