0Pricing
SwiftUI Academy · 课时

滑动删除行

使用 onDelete 删除项目。

滑动删除行 是 CoddyKit 上的免费 SwiftUI Academy 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 SwiftUI Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 SwiftUI Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Lists That Can Lose Rows

A static list just shows data. Real apps let people remove items, and SwiftUI makes deletion feel native with a simple swipe. ✨

Meet onDelete

The onDelete modifier attaches to a ForEach inside a List. It enables the swipe-left gesture that reveals a red Delete button.

Your Data Lives in State

To delete a row you must own the data. Keep your array in @State so changing it instantly refreshes the list on screen.

struct Item: Identifiable {
    let id = UUID()
    var name: String
}

Declaring the Array

Hold your items in a mutable @State array. When you remove an element, SwiftUI animates the row away for free.

@State private var items = [
    Item(name: "Milk"),
    Item(name: "Eggs")
]

The IndexSet Argument

onDelete hands you an IndexSet of the rows the user swiped. It is a set because a person could remove several at once.

Removing by Offsets

Pass that IndexSet straight to the arrays remove(atOffsets:) method. One line removes exactly the swiped items safely.

func delete(at offsets: IndexSet) {
    items.remove(atOffsets: offsets)
}

Wiring It Together

Put your rows in a ForEach inside a List, then attach onDelete to call your delete function. That is the whole pattern.

List {
    ForEach(items) { item in
        Text(item.name)
    }
    .onDelete(perform: delete)
}

Inline Closures Work Too

You can skip the named function and write the deletion right inside onDelete as a closure when the logic is tiny.

.onDelete { offsets in
    items.remove(atOffsets: offsets)
}

Free Swipe Animation

Because items is @State, SwiftUI compares before and after and slides the deleted row out smoothly. You never write the animation yourself.

Attach to ForEach, Not List

A common slip is putting onDelete on the List. It belongs on the ForEach, since that is what maps your array to rows.

Custom Swipe Actions

For more than delete, reach for swipeActions, which lets you add tinted buttons like Archive or Flag on either edge.

.swipeActions {
    Button("Flag") { }
        .tint(.orange)
}

Quick Check

Where does the onDelete modifier belong?

Recap: Swipe-to-Delete

You learned to keep items in @State, attach onDelete to a ForEach, and remove swiped rows with remove(atOffsets:). 🎉

常见问题解答

「滑动删除行」课时是免费的吗?

是的 — 「滑动删除行」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 SwiftUI Academy 课程的其余内容,请升级到 CoddyKit PRO。 SwiftUI Academy 课程共包含 4 节课。

「滑动删除行」这节课中我会学到什么?

使用 onDelete 删除项目。 你通过在浏览器中直接运行的动手代码来练习 SwiftUI Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 SwiftUI Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 SwiftUI Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「滑动删除行」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 SwiftUI Academy 课中编写并运行代码吗?

能。每节 SwiftUI Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 滑动删除行
  2. 使用 onMove 重新排序
  3. 添加新项目
  4. 下拉刷新
← 返回 SwiftUI Academy