0
0
iOS Swiftmobile~20 mins

Swipe actions in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 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.