Challenge - 5 Problems
Swipe Action Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
Swipe action behavior in a UITableView
What happens when you swipe a table view cell to the left and implement the following code in
trailingSwipeActionsConfigurationForRowAt?iOS Swift
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let deleteAction = UIContextualAction(style: .destructive, title: "Delete") { _, _, completionHandler in
print("Deleted row at \(indexPath.row)")
completionHandler(true)
}
return UISwipeActionsConfiguration(actions: [deleteAction])
}Attempts:
2 left
💡 Hint
Trailing swipe actions appear when swiping left on a cell.
✗ Incorrect
The method
trailingSwipeActionsConfigurationForRowAt defines actions shown when swiping left. The action style .destructive shows a red button. The completion handler confirms the action was performed.❓ lifecycle
intermediate2:00remaining
Swipe action completion handler importance
What is the effect of not calling
completionHandler(true) inside a swipe action handler?iOS Swift
let editAction = UIContextualAction(style: .normal, title: "Edit") { _, _, completionHandler in print("Edit tapped") // Missing completionHandler call }
Attempts:
2 left
💡 Hint
The completion handler tells the system the action finished.
✗ Incorrect
If you do not call the completion handler, the system thinks the action is still running and keeps the swipe action visible, preventing the swipe from closing.
📝 Syntax
advanced2:00remaining
Correct syntax for adding multiple swipe actions
Which option correctly creates two swipe actions "Flag" and "More" for trailing swipe in a UITableView?
iOS Swift
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let flagAction = UIContextualAction(style: .normal, title: "Flag") { _, _, completionHandler in
completionHandler(true)
}
let moreAction = UIContextualAction(style: .normal, title: "More") { _, _, completionHandler in
completionHandler(true)
}
// Return configuration here
}Attempts:
2 left
💡 Hint
The actions parameter expects an array of UIContextualAction objects.
✗ Incorrect
The
actions parameter must be an array. Option B correctly passes an array with two actions. Other options have syntax errors or wrong types.advanced
2:00remaining
Swipe action triggering navigation
You want a swipe action to navigate to a detail screen when tapped. Which code snippet correctly performs this navigation inside the swipe action handler?
iOS Swift
let detailAction = UIContextualAction(style: .normal, title: "Details") { [weak self] _, _, completionHandler in // Navigate to detail screen completionHandler(true) }
Attempts:
2 left
💡 Hint
Use optional chaining with self inside closures to avoid retain cycles.
✗ Incorrect
Inside closures, use [weak self] and optional chaining to safely access navigationController. Option A correctly uses self? and optional chaining. Options C and D cause errors because self is optional or missing ?. Option A is invalid because present() is not called on self or a view controller.
🔧 Debug
expert2:00remaining
Why does swipe action not appear on iOS 15+?
You implemented
leadingSwipeActionsConfigurationForRowAt but the swipe action does not appear on iOS 15 and later. What is the most likely cause?iOS Swift
func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let pinAction = UIContextualAction(style: .normal, title: "Pin") { _, _, completionHandler in
completionHandler(true)
}
return UISwipeActionsConfiguration(actions: [pinAction])
}Attempts:
2 left
💡 Hint
Check the table view cell layout and margins if swipe actions do not appear.
✗ Incorrect
On iOS 15+, if the cell's separatorInset or layoutMargins are set incorrectly, swipe actions may not appear. This is a common layout issue. Swipe actions are not disabled by default and the method is not deprecated. Style .normal is supported.