Your Second iOS & SwiftUI App

Nov 4 2021 Swift 5.5, iOS 15, Xcode 13

Part 3: Managing Rows

25. Sections

Lesson Complete

Play Next Lesson
Next
Save for later
About this episode
See versions

See course reviews

See forum comments
Cinema mode Mark as Complete Download course materials
Previous episode: 24. Environment Values Next episode: 26. Swipe Actions

This video was last updated on Nov 4 2021

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a kodeco.com Professional subscription.

Some of your books, you’ve read. Some, you have not! A useful organizational feature of your app would be to section those off from each other.

enum Section {
  case readMe
  case finished
}

class Library: ObservableObject {
enum Section🟩: CaseIterable {

  var sortedBooks: [Section: [Book]] = [:]

  /// Adds a new book at the start of the library's manually-sorted books.
  func addNewBook(_ book: Book, image: UIImage?) {
  func sortBooks() {
    let groupedBooks = Dictionary(grouping: booksCache, by: \.readMe)
  }
    let groupedBooks = Dictionary(grouping: booksCache, by: \.readMe)
    🟩return Dictionary(
      uniqueKeysWithValues: groupedBooks.map {
        (($0.key ? .readMe : .finished), $0.value)
      }
    )
🟩struct SectionView: View {
  var body: some View {

  }
}

struct ContentView_Previews: PreviewProvider {
private struct SectionView: View {
private struct BookRow: View {
private struct SectionView: View {
  let section: Section

  var body: some View {
  let section: Section
  @EnvironmentObject var library: Library

  var body: some View {
  var body: some View {
    if let books = library.sortedBooks[section] {

    }
  }
    if let books = library.sortedBooks[section] {
      SwiftUI.Section(content: <#T##() -> _#>, header: <#T##_#>)
    }
      SwiftUI.Section {
        🟩ForEach(books) { book in
          BookRow(book: book)
        }
      } header: {
      } header: {
       🟩Image("BookTexture")
    }
        ForEach(library.sortedBooks) { book in
          BookRow(book: book)
        }
          content: NewBookView.init
        )


      }
      .navigationBarTitle("My Library")
        )

        ForEach(Section.allCases, id: <#T##KeyPath<_.Element, _>#>, content: <#T##(_.Element) -> _#>)
      }
ForEach(Section.allCases, id: \.self, content: <#T##(_.Element) -> _#>)
        ForEach(Section.allCases, id: \.self) {
          SectionView(section: $0)
        }
      SwiftUI.Section(
        header:
          Image("BookTexture")
          .resizable()
          .scaledToFit()
      ) {
          .scaledToFit()
          .listRowInsets(/*@START_MENU_TOKEN@*/.none/*@END_MENU_TOKEN@*/)
      ) {
.listRowInsets(.init())
  @EnvironmentObject var library: Library

  var title: String {

  }

  var body: some View {
  var title: String {
    switch section {

    }
  }
  var title: String {
    switch section {
    case .readMe:
      <#code#>
    case .finished:
      <#code#>
    }
  }
    case .readMe:
      return "Read Me!"
    case .finished:
      return "Finished!"
    }
        header:
          ZStack {
            Image("BookTexture")
              .resizable()
              .scaledToFit()
              .listRowInsets(.init())
          }
      ) {
              .scaledToFit()
          }
          .listRowInsets(.init())
      ) {
        ForEach(books) {
              .scaledToFit()
            Text(title)
          }
            Text(title)
              .font(.custom("American Typewriter", size: <#T##CGFloat#>))
          }
size: 24))
.foregroundColor(.primary)
  func sortBooks() {

  }
objectWillChange.send()
booksCache = sortedBooks
booksCache = sortedBooks🟩.flatMap { $0.value }
        sortedBooks  
        🟩.sorted { $1.key == .finished }
        .flatMap { $0.value }
Button {
          book.readMe.toggle()
          library.sortBooks()
        } label: {
Button {
          book.readMe.toggle()
          ❌library.sortBooks()
.padding()
🟩.onDisappear {    
  library.sortBooks()
}
.onDisappear {
  🟩withAnimation {      
    library.sortBooks()
  }
}