Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
- {
- let clearAction = UIContextualAction(style: .normal, title: "Clear Data") { (contextAction: UIContextualAction, sourceView: UIView, completionHandler: (Bool) -> Void) in
- print("Clear Action Tapped")
- // Delete the created images
- let fileNameToDeletePri = "(self.bonus?.bonusCode ?? "XXX")_1.jpg"
- let fileNameToDeleteOpt = "(self.bonus?.bonusCode ?? "XXX")_2.jpg"
- var filePathPri = ""
- var filePathOpt = ""
- // Find documents directory on device
- let dirs : [String] = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.allDomainsMask, true)
- if dirs.count > 0 {
- let dir = dirs[0] //documents directory
- filePathPri = dir.appendingFormat("/" + fileNameToDeletePri)
- filePathOpt = dir.appendingFormat("/" + fileNameToDeleteOpt)
- print("Local path = (filePathPri)")
- print("Local path = (filePathOpt)")
- } else {
- print("Could not find local directory to store file")
- return
- }
- do {
- let fileManager = FileManager.default
- // Check if primary file exists
- if fileManager.fileExists(atPath: filePathPri) {
- // Delete file
- try fileManager.removeItem(atPath: filePathPri)
- } else {
- print("Primary image does not exist")
- }
- // Check if optional file exists
- if fileManager.fileExists(atPath: filePathOpt) {
- // Delete file
- try fileManager.removeItem(atPath: filePathOpt)
- } else {
- print("Optional image does not exist")
- }
- }
- catch let error as NSError {
- print("An error took place: (error)")
- }
- completionHandler(true)
- }
- clearAction.backgroundColor = .blue
- let swipeConfig = UISwipeActionsConfiguration(actions: [clearAction])
- return swipeConfig
- }
Add Comment
Please, Sign In to add comment