Your Second iOS & SwiftUI App

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

Part 3: Managing Rows

27. Delete & Move Rows

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: 26. Swipe Actions Next episode: 28. Conclusion

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've reached locked video content where the transcript will be shown as obfuscated text.

To recap, we’re going to let users delete books from their library in two ways: first, the swipe action we already set up, and second, via an edit button.

        🟩.onDelete(perform: { indexSet in
          /*@START_MENU_TOKEN@*//*@PLACEHOLDER=Code@*/ /*@END_MENU_TOKEN@*/
        })
        .labelStyle...
      }
          BookRow(book: $0)
        }
        .onDelete { indexSet in

        }
      }
func deleteBook🟩s🟥() {
func deleteBooks(🟩atOffsets offsets: IndexSet) {
func deleteBooks(atOffsets offsets: IndexSet🟩, section: Section) {
      sortedBooks[section]
      // TODO: Remove Image
sortedBooks[section]?.remove(atOffsets: <#T##IndexSet#>)
remove(atOffsets: offsets)
  var sortedBooks: [Section: [Book]] 🟩{
    get {
      ...
    }
    set {

    }
  }🟥
set {
🟩booksCache =
      sortedBooks
      .sorted { $1.key == .finished }
      .flatMap { $0.value }
    set {
      booksCache = newValue
    }
  func deleteBooks(atOffsets offsets: IndexSet, section: Section?) {
    🟩let booksBeforeDeletion = booksCache

      sortedBooks.remove(atOffsets: offsets)
    }

    🟩booksCache.difference(from: booksBeforeDeletion)
  }
    for change in booksCache.difference(from: booksBeforeDeletion) {

    }
  }
    for change in booksCache.difference(from: booksBeforeDeletion) {
      switch change {
      
      }
    }
    for change in booksCache.difference(from: booksBeforeDeletion) {
      switch change {
      case .insert(offset: let offset, element: let element, associatedWith: let associatedWith):
        <#code#>
      case .remove(offset: let offset, element: let element, associatedWith: let associatedWith):
        <#code#>
      }
    }
      switch change {
      case .remove(offset: let offset, element: let element, associatedWith: let associatedWith):
        <#code#>
      }
      if case .remove(offset: let offset, element: let element, associatedWith: let associatedWith)💰 = change {
        <#code#>
      }
if case .remove(_, let element, _) = change {
(_, let deletedBook, _)
      if case .remove(_, let deletedBook, _) = change {
        uiImages[deletedBook] = nil
      }
            .swipeActions(edge: .trailing) {
              Button(role: .destructive) {
                guard let index = books.firstIndex(where: {$0.id == book.id})
                else { return }
                
                withAnimation {
                  library.deleteBooks(atOffsets: .init(integer: index), section: section)
                }
        .onDelete { indexSet in
            withAnimation {
                  library.deleteBooks(atOffsets: .init(integer: index), section: section)
                }
        }
        ForEach(Section.allCases, id: \.self) {
          SectionView(section: $0)
        }
      }
      🟩.toolbar(content: <#T##() -> ToolbarContent#>)
      .navigationTitle("My Library")
.toolbar(content: 🟩EditButton.init)
        .onDelete { indexSet in
          ...
        }
        .onMove { indices, newOffset in

        }
      }
  }

  func moveBooks(
  
  ) {

  }

  /// Load, save, or delete an image corresponding to a book's title and author.
  func moveBooks(
    oldOffsets: IndexSet
  ) {
  func moveBooks(
    oldOffsets: IndexSet, newOffset: Int
  ) {
  func moveBooks(
    oldOffsets: IndexSet, newOffset: Int,
    section: Section
  ) {
    section: Section
  ) {
    sortedBooks[section]
  }
  ) {
    sortedBooks[section]?.move(fromOffsets: oldOffsets, toOffset: newOffset)
  }
        .onMove { indices, newOffset in
          library.moveBooks(
            oldOffsets: indices, newOffset: newOffset,
            section: section
          )
        }