0Pricing
SwiftUI Academy · درس

حذف الصفوف بالسحب

أزل العناصر باستخدام onDelete

حذف الصفوف بالسحب درس مجاني في SwiftUI Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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:). 🎉

الأسئلة الشائعة

هل درس «حذف الصفوف بالسحب» مجاني؟

نعم — نص درس «حذف الصفوف بالسحب» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة SwiftUI Academy، انتقل إلى CoddyKit PRO. تتضمن دورة SwiftUI Academy 4 دروس في المجموع.

ماذا ستتعلم في «حذف الصفوف بالسحب»؟

أزل العناصر باستخدام onDelete تتمرن على SwiftUI Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ SwiftUI Academy؟

لا تُشترط خبرة سابقة. SwiftUI Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.

كم من الوقت يستغرق درس «حذف الصفوف بالسحب»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس SwiftUI Academy هذا؟

نعم. كل درس في SwiftUI Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. حذف الصفوف بالسحب
  2. إعادة الترتيب باستخدام onMove
  3. إضافة عناصر جديدة
  4. السحب للتحديث
← العودة إلى SwiftUI Academy