Your Second iOS & SwiftUI App

Nov 4 2021 Swift 5.5, iOS 15, Xcode 13

Part 3: Managing Rows

27. Delete & Move Rows

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

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.

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