Skip to content

Instantly share code, notes, and snippets.

@rodydavis
Last active November 9, 2017 12:47
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 rodydavis/274fb01c1aaf298ae94e1387e57c91c4 to your computer and use it in GitHub Desktop.
Save rodydavis/274fb01c1aaf298ae94e1387e57c91c4 to your computer and use it in GitHub Desktop.
Call, Email and Delete Cell Actions (Left and Right Swipe)
override func tableView(_ tableView: UITableView, editActionsForRowAt: IndexPath) -> [UITableViewRowAction]? {
let indexPathIndex = tableView.indexPathForSelectedRow
let indexPath = indexPathIndex?.row ?? 0
let callButton = UITableViewRowAction(style: .normal, title: "📞\n Call") { action, index in
print("call button tapped")
let number = data[indexPath].phone ?? ""
print("Number: " + number)
UIApplication.makeAPhoneCall(number: number.replacingOccurrences(of: "-", with: ""))
}
callButton.backgroundColor = UIColor.init(red: 144/255, green: 238/255, blue: 144/255, alpha: 1)
let emailButton = UITableViewRowAction(style: .normal, title: "✉️\n Email") { action, index in
print("email button tapped")
let email = data[indexPath].email ?? ""
print("Email: " + email)
UIApplication.sendEmail(to: email, body: "")
print("Email Sent")
}
emailButton.backgroundColor = UIColor.init(red: 1, green: 186/255, blue: 64/255, alpha: 1)
let deleteButton = UITableViewRowAction(style: .normal, title: "❌\n Delete") { action, index in
print("delete button tapped")
let alert = UIAlertController(title: "Wairing", message: "Are you sure you want to delete?", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Delete", style: UIAlertActionStyle.destructive, handler: { action in
// do something like...
//self.launchMissile()
print(".....Deleted")
}))
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
}
deleteButton.backgroundColor = UIColor.init(red: 1, green: 102/255, blue: 102/255, alpha: 1)
return [deleteButton, emailButton, callButton] //
}
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
// MARK: - UITableViewDelegate Protocol
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let deleteAction = UIContextualAction(style: .destructive, title: "Delete") { (action, sourceView, completionHandler) in
// Delete the row from the data store
if let appDelegate = (UIApplication.shared.delegate as? AppDelegate) {
let context = appDelegate.persistentContainer.viewContext
let restaurantToDelete = self.fetchResultController.object(at: indexPath)
context.delete(restaurantToDelete)
appDelegate.saveContext()
}
// Call completion handler with true to indicate
completionHandler(true)
}
let shareAction = UIContextualAction(style: .normal, title: "Share") { (action, sourceView, completionHandler) in
let defaultText = "Just checking in at " + self.restaurants[indexPath.row].name!
let activityController: UIActivityViewController
if let restaurantImage = self.restaurants[indexPath.row].image,
let imageToShare = UIImage(data: restaurantImage as Data) {
activityController = UIActivityViewController(activityItems: [defaultText, imageToShare], applicationActivities: nil)
} else {
activityController = UIActivityViewController(activityItems: [defaultText], applicationActivities: nil)
}
if let popoverController = activityController.popoverPresentationController {
if let cell = tableView.cellForRow(at: indexPath) {
popoverController.sourceView = cell
popoverController.sourceRect = cell.bounds
}
}
self.present(activityController, animated: true, completion: nil)
completionHandler(true)
}
// Customize the action buttons
deleteAction.backgroundColor = UIColor(red: 231.0/255.0, green: 76.0/255.0, blue: 60.0/255.0, alpha: 1.0)
deleteAction.image = UIImage(named: "delete")
shareAction.backgroundColor = UIColor(red: 254.0/255.0, green: 149.0/255.0, blue: 38.0/255.0, alpha: 1.0)
shareAction.image = UIImage(named: "share")
let swipeConfiguration = UISwipeActionsConfiguration(actions: [deleteAction, shareAction])
return swipeConfiguration
}
override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let checkInAction = UIContextualAction(style: .normal, title: "Check-in") { (action, sourceView, completionHandler) in
let cell = tableView.cellForRow(at: indexPath) as! RestaurantTableViewCell
self.restaurants[indexPath.row].isVisited = (self.restaurants[indexPath.row].isVisited) ? false : true
cell.heartImageView.isHidden = self.restaurants[indexPath.row].isVisited ? false : true
completionHandler(true)
}
// Customize the action button
checkInAction.backgroundColor = UIColor(red: 39, green: 174, blue: 96)
checkInAction.image = self.restaurants[indexPath.row].isVisited ? UIImage(named: "undo") : UIImage(named: "tick")
let swipeConfiguration = UISwipeActionsConfiguration(actions: [checkInAction])
return swipeConfiguration
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment