Bird
Raised Fist0
iOS Swiftmobile~20 mins

Swipe actions in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Swipe Action Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate
2: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])
}
AA red "Delete" button appears when swiping left, and tapping it prints the deleted row index.
BA green "Delete" button appears when swiping left, but tapping it does nothing.
CSwiping left does nothing because the method is for right swipe only.
DSwiping right triggers the delete action instead of left swipe.
Attempts:
2 left
💡 Hint
Trailing swipe actions appear when swiping left on a cell.
❓ lifecycle
intermediate
2: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
}
AThe swipe action closes immediately without performing the action.
BThe swipe action triggers twice because completion is not called.
CThe app crashes due to missing completion handler call.
DThe swipe action button stays visible and the swipe does not close automatically.
Attempts:
2 left
💡 Hint
The completion handler tells the system the action finished.
📝 Syntax
advanced
2: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
}
Areturn UISwipeActionsConfiguration(actions: flagAction, moreAction)
Breturn UISwipeActionsConfiguration(actions: [flagAction, moreAction])
Creturn UISwipeActionsConfiguration(actions: flagAction + moreAction)
Dreturn UISwipeActionsConfiguration(actions: [flagAction & moreAction])
Attempts:
2 left
💡 Hint
The actions parameter expects an array of UIContextualAction objects.
❓ navigation
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)
}
Aself?.navigationController?.pushViewController(DetailViewController(), animated: true)
Bpresent(DetailViewController(), animated: true)
CnavigationController.pushViewController(DetailViewController(), animated: true)
Dself.navigationController.pushViewController(DetailViewController(), animated: true)
Attempts:
2 left
💡 Hint
Use optional chaining with self inside closures to avoid retain cycles.
🔧 Debug
expert
2: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])
}
AThe method <code>leadingSwipeActionsConfigurationForRowAt</code> is deprecated and replaced by a delegate property.
BSwipe actions are disabled by default on iOS 15 and require explicit enabling in Info.plist.
CThe table view's <code>separatorInset</code> or <code>layoutMargins</code> prevent swipe actions from showing.
DThe swipe action style .normal is not supported on iOS 15 and must be .destructive.
Attempts:
2 left
💡 Hint
Check the table view cell layout and margins if swipe actions do not appear.

Practice

(1/5)
1. What is the main purpose of using UIContextualAction in a swipe action on iOS?
easy
A. To handle tap gestures on the entire screen
B. To create buttons that appear when the user swipes a table cell
C. To change the background color of the app
D. To add animations to buttons outside of swipe actions

Solution

  1. Step 1: Understand swipe actions in iOS

    Swipe actions allow users to reveal buttons by swiping on a list item.
  2. Step 2: Role of UIContextualAction

    UIContextualAction creates the buttons shown during the swipe gesture.
  3. Final Answer:

    To create buttons that appear when the user swipes a table cell -> Option B
  4. Quick Check:

    UIContextualAction = Swipe buttons [OK]
Hint: UIContextualAction = swipe buttons on list items [OK]
Common Mistakes:
  • Confusing swipe action buttons with general tap gestures
  • Thinking UIContextualAction changes app colors
  • Assuming it adds animations unrelated to swipe
2. Which of the following is the correct way to create a UIContextualAction for a swipe action in Swift?
easy
A. let action = UIContextualAction(style: .normal, title: "Delete", handler: { _, _, completionHandler in completionHandler(true) })
B. let action = UIContextualAction(style: "normal", title: "Delete", handler: { _, _, completionHandler in completionHandler(true) })
C. let action = UIContextualAction(style: .normal, title: Delete, handler: { _, _, completionHandler in completionHandler(true) })
D. let action = UIContextualAction(style: .normal, title: "Delete")

Solution

  1. Step 1: Check UIContextualAction initializer syntax

    The initializer requires a style enum, a String title, and a handler closure with parameters.
  2. Step 2: Validate each option

    let action = UIContextualAction(style: .normal, title: "Delete", handler: { _, _, completionHandler in completionHandler(true) }) uses correct enum style, quotes around title, and proper handler closure with completion call.
  3. Final Answer:

    let action = UIContextualAction(style: .normal, title: "Delete", handler: { _, _, completionHandler in completionHandler(true) }) -> Option A
  4. Quick Check:

    Correct initializer syntax = let action = UIContextualAction(style: .normal, title: "Delete", handler: { _, _, completionHandler in completionHandler(true) }) [OK]
Hint: Use .normal enum and quoted title with handler closure [OK]
Common Mistakes:
  • Using string instead of enum for style
  • Missing quotes around title string
  • Omitting handler closure or completion call
3. Given this code snippet inside a UITableViewDelegate method, what happens when the user swipes the cell?
let deleteAction = UIContextualAction(style: .destructive, title: "Delete") { _, _, completionHandler in
    print("Deleted")
    completionHandler(true)
}
let configuration = UISwipeActionsConfiguration(actions: [deleteAction])
return configuration
medium
A. A red "Delete" button appears; tapping it prints "Deleted" and closes swipe
B. The cell immediately deletes without user interaction
C. Nothing happens because completionHandler is not called
D. The swipe gesture is disabled

Solution

  1. Step 1: Analyze swipe action setup

    A destructive style button labeled "Delete" is created with a handler that prints and calls completionHandler(true).
  2. Step 2: Understand user interaction

    Swiping reveals the button; tapping it triggers the handler, printing "Deleted" and ending the swipe.
  3. Final Answer:

    A red "Delete" button appears; tapping it prints "Deleted" and closes swipe -> Option A
  4. Quick Check:

    Swipe button tap triggers handler = A red "Delete" button appears; tapping it prints "Deleted" and closes swipe [OK]
Hint: Completion handler true ends swipe and triggers action [OK]
Common Mistakes:
  • Thinking swipe deletes immediately without tap
  • Forgetting to call completionHandler(true)
  • Assuming swipe gesture disables without code
4. Identify the error in this swipe action handler code:
let editAction = UIContextualAction(style: .normal, title: "Edit") { action, view, _ in
    print("Edit tapped")
}
medium
A. Title string should be lowercase
B. Incorrect style enum used for edit action
C. Missing call to completionHandler inside the closure
D. Closure parameters are in wrong order

Solution

  1. Step 1: Review UIContextualAction handler requirements

    The handler closure must call the completionHandler to signal action completion.
  2. Step 2: Check the given code

    The closure ignores the third parameter (completionHandler) and does not call it, causing swipe to hang.
  3. Final Answer:

    Missing call to completionHandler inside the closure -> Option C
  4. Quick Check:

    Always call completionHandler in swipe action handler [OK]
Hint: Always call completionHandler(true/false) in handler closure [OK]
Common Mistakes:
  • Ignoring completionHandler call
  • Using wrong style enum (normal is valid here)
  • Thinking title case matters
  • Mixing closure parameter order
5. You want to add two swipe actions: "Flag" (normal style) and "Delete" (destructive style) to a table cell. Which code correctly creates and returns the swipe configuration?
hard
A. let flag = UIContextualAction(style: .normal, title: "Flag") { _, _, completion in completion(true) } let delete = UIContextualAction(style: .destructive, title: "Delete") { _, _, completion in completion(true) } return UISwipeActionsConfiguration(actions: [flag, delete])
B. let flag = UIContextualAction(style: .normal, title: "Flag") { _, _, completionHandler in completionHandler(true) } let delete = UIContextualAction(style: .destructive, title: "Delete") { _, _, completionHandler in completionHandler(true) } return UISwipeActionsConfiguration(actions: [flag, delete])
C. let flag = UIContextualAction(style: .normal, title: "Flag") { _, _, completionHandler in completionHandler(false) } let delete = UIContextualAction(style: .destructive, title: "Delete") { _, _, completionHandler in completionHandler(true) } return UISwipeActionsConfiguration(actions: [flag, delete])
D. let flag = UIContextualAction(style: .normal, title: "Flag") { _, _, completionHandler in completionHandler(true) } let delete = UIContextualAction(style: .destructive, title: "Delete") { _, _, completionHandler in completionHandler(true) } return UISwipeActionsConfiguration(actions: [delete, flag])

Solution

  1. Step 1: Confirm action creation and completion calls

    Both actions use correct styles and call completionHandler(true) to indicate success.
  2. Step 2: Check order of actions in configuration

    Swipe actions appear in order given; "Delete" usually placed first for prominence, then "Flag".
  3. Final Answer:

    Actions created with correct styles and completion calls; configuration returns [delete, flag] -> Option D
  4. Quick Check:

    Correct styles, completion calls, and order = let flag = UIContextualAction(style: .normal, title: "Flag") { _, _, completionHandler in completionHandler(true) } let delete = UIContextualAction(style: .destructive, title: "Delete") { _, _, completionHandler in completionHandler(true) } return UISwipeActionsConfiguration(actions: [delete, flag]) [OK]
Hint: Call completionHandler(true) and order destructive before normal [OK]
Common Mistakes:
  • Calling completionHandler(false) which cancels action
  • Using wrong variable name for completionHandler
  • Incorrect order of actions in array
  • Omitting completionHandler call
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard