Guest User

Untitled

a guest
Jul 15th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
  2. {
  3. let clearAction = UIContextualAction(style: .normal, title: "Clear Data") { (contextAction: UIContextualAction, sourceView: UIView, completionHandler: (Bool) -> Void) in
  4. print("Clear Action Tapped")
  5. // Delete the created images
  6. let fileNameToDeletePri = "(self.bonus?.bonusCode ?? "XXX")_1.jpg"
  7. let fileNameToDeleteOpt = "(self.bonus?.bonusCode ?? "XXX")_2.jpg"
  8. var filePathPri = ""
  9. var filePathOpt = ""
  10. // Find documents directory on device
  11. let dirs : [String] = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.allDomainsMask, true)
  12. if dirs.count > 0 {
  13. let dir = dirs[0] //documents directory
  14. filePathPri = dir.appendingFormat("/" + fileNameToDeletePri)
  15. filePathOpt = dir.appendingFormat("/" + fileNameToDeleteOpt)
  16. print("Local path = (filePathPri)")
  17. print("Local path = (filePathOpt)")
  18. } else {
  19. print("Could not find local directory to store file")
  20. return
  21. }
  22. do {
  23. let fileManager = FileManager.default
  24.  
  25. // Check if primary file exists
  26. if fileManager.fileExists(atPath: filePathPri) {
  27. // Delete file
  28. try fileManager.removeItem(atPath: filePathPri)
  29. } else {
  30. print("Primary image does not exist")
  31. }
  32. // Check if optional file exists
  33. if fileManager.fileExists(atPath: filePathOpt) {
  34. // Delete file
  35. try fileManager.removeItem(atPath: filePathOpt)
  36. } else {
  37. print("Optional image does not exist")
  38. }
  39. }
  40. catch let error as NSError {
  41. print("An error took place: (error)")
  42. }
  43. completionHandler(true)
  44. }
  45.  
  46. clearAction.backgroundColor = .blue
  47. let swipeConfig = UISwipeActionsConfiguration(actions: [clearAction])
  48. return swipeConfig
  49. }
Add Comment
Please, Sign In to add comment