Your Second iOS & SwiftUI App

Nov 4 2021 · Swift 5.5, iOS 15, Xcode 13

Part 3: Managing Rows

25. Sections

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

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

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

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()
  }
}