Add swipe to delete UITableViewCell
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Swipe-to-delete in UITableView is a standard iOS interaction pattern that users expect in list interfaces. UIKit provides built-in APIs for this behavior, but production code also needs data consistency, undo support, and safe async deletion flows. This guide covers modern implementations for both basic and custom swipe actions.
Basic Swipe-to-Delete With Data Source API
The classic API is tableView(_:commit:forRowAt:). When the user confirms delete, remove the model item first, then update table rows.
This is enough for simple local data lists.
Custom Swipe Actions With UIContextualAction
For modern UI customization, use trailing swipe actions. This enables delete plus extra actions such as archive or flag.
This API is preferred for richer interaction design.
Keeping Model and UI in Sync
Deletion bugs usually come from updating UI and model in inconsistent order. Always mutate your source of truth first, then animate row deletion.
For diffable data sources, do not call deleteRows manually. Instead, apply an updated snapshot.
Mixing diffable updates with manual row mutations can crash with invalid update exceptions.
Async Server Deletion Pattern
If deletion requires an API call, decide whether to use optimistic or pessimistic UI update:
- optimistic: remove immediately, rollback on failure
- pessimistic: wait for server success, then remove
Optimistic example outline:
This keeps UI responsive but requires robust rollback handling.
Add Undo for Better UX
Users frequently trigger swipe delete by mistake. An undo option improves trust.
Common patterns:
- show snackbar style message with undo action
- support system undo manager for editable content
- keep deleted item cached briefly before hard delete
For permanent destructive actions, also consider confirmation for sensitive records.
Accessibility and Interaction Quality
Swipe actions should remain accessible to VoiceOver users through rotor actions and explicit action buttons where appropriate. Test on smaller devices to ensure action titles remain visible and do not truncate confusingly.
Animation quality matters too. Batch updates should remain short and deterministic to avoid jank in long lists.
Common Pitfalls
- Deleting table rows without deleting matching model entries.
- Mixing diffable data source updates with manual row deletion APIs.
- Capturing stale index paths in async callbacks after data changes.
- Ignoring server failure rollback when using optimistic deletion.
- Shipping destructive swipe actions without undo or confirmation where needed.
Summary
- UIKit provides built-in swipe-to-delete APIs for both basic and custom behaviors.
- Keep model updates and table updates strictly synchronized.
- Use
UIContextualActionfor modern custom swipe actions. - Handle async deletion carefully with rollback or delayed removal strategy.
- Add undo and accessibility support for production-quality interaction.

