Skip to content

Instantly share code, notes, and snippets.

@vulgur
Created August 11, 2018 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vulgur/a48c454676f3d013c1ce32474825122b to your computer and use it in GitHub Desktop.
Save vulgur/a48c454676f3d013c1ce32474825122b to your computer and use it in GitHub Desktop.
[Leading and Trailing Action on Cell] #swipe
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
guard let product = dataStore?.products[indexPath.row] else {
return nil
}
let addAction = UIContextualAction(style: .normal, title: "Add") { [weak self](action, view, completionHandler) in
guard let `self` = self else {
completionHandler(false)
return
}
self.listController?.addItem(named: product.name)
completionHandler(true)
}
addAction.backgroundColor = UIColor.ggGreen
let configuration = UISwipeActionsConfiguration(actions: [addAction])
return configuration
}
override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
guard let product = dataStore?.products[indexPath.row] else {
return nil
}
let copyAction = UIContextualAction(style: .normal, title: "Copy") { (action, view, completionHandler) in
let data = NSKeyedArchiver.archivedData(withRootObject: product)
UIPasteboard.general.setData(data, forPasteboardType: Product.productTypeId)
completionHandler(true)
}
copyAction.backgroundColor = UIColor.ggDarkGreen
let configuration = UISwipeActionsConfiguration(actions: [copyAction])
return configuration
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment