1
votes

I currently have my UITableView revealing the "Delete" button when you swipe left on a table view cell. I want to hide this button with a swipe right on that same cell. It should be sticky too so that it hides as I swipe right - just like how it reveals the "Delete" button as you swipe left. Right now, when I swipe left to reveal the delete button, I can only get it to hide when I scroll my table view. Is there a built-in way to achieve this swipe-right-to-hide behavior? Or does this need to be a custom built solution?

EDIT: I thought the defualt Messages app had this behavior, but I realized that when I was swiping right, I was scrolling the tableview a little bit which caused it to hide. The only way to hide right now is tapping the cell or scrolling the tableview

I've set up my tableView to show the delete button by implementing the following method:

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
   if editingStyle == .delete {
        print("deleted!")
   }     
}
1
Can share screenshot? how the output will be ?McDonal_11

1 Answers

1
votes

Yeah, I too was having an issue trying to allow users to swipe away the Delete option. (Similar to how Apple's Mail app works)

The only way I found it to work as expected was if you also include a leadingSwipeAction... (If the user initially swipes either right or left they get different options). Once you add the other side, then users can swipe the Delete away.

Incidentally "Swipe the Delete Away" sounds like a dope emo song.

Here is code I currently have that is almost working perfectly. There is an occasional graphical anomaly with the leading button jumping to the right, but I think that might be due to some other code in my view. This code is borrowed heavily from https://chariotsolutions.com/blog/post/uitableview-swipe-actions-in-ios-11/

    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {

    if indexPath.section == Slider.Section.results.rawValue {
        return true
    } else {
        return false
    }

}

override func tableView(_ tableView: UITableView,
                        leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
{

    let favoriteAction = self.contextualToggleFavoriteAction(forRowAtIndexPath: indexPath)

    return UISwipeActionsConfiguration(actions: [favoriteAction])

}

override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {


    let delete = UIContextualAction(style: .destructive,
                                    title: "Delete") { (contextAction: UIContextualAction, sourceView: UIView, completionHandler: (Bool) -> Void) in

                                        Filter.allMarkers.remove(at: indexPath.row)
                                        tableView.deleteRows(at: [indexPath], with: .fade)
                                        completionHandler(true)

    }

    let share = UIContextualAction(style: .normal,
                                    title: "Share") { (contextAction: UIContextualAction, sourceView: UIView, completionHandler: (Bool) -> Void) in

                                        print("About to share -> \(Filter.allMarkers[indexPath.row].title ?? "")")
                                        completionHandler(true)

    }

    share.backgroundColor = Color.ocean

    let swipeConfig = UISwipeActionsConfiguration(actions: [delete, share])
    return swipeConfig
}


func contextualToggleFavoriteAction(forRowAtIndexPath indexPath: IndexPath) -> UIContextualAction {

    let marker = Filter.allMarkers[indexPath.row]
    let action = UIContextualAction(style: .normal,
                                    title: "Favorite") { (contextAction: UIContextualAction, sourceView: UIView, completionHandler: (Bool) -> Void) in
                                        if marker.toggleFavoriteFlag() {
                                            completionHandler(true)
                                        } else {
                                            completionHandler(false)
                                        }
    }

    //action.image = UIImage(named: "flag")
    action.backgroundColor = marker.isFavorite ? Color.magenta : UIColor.gray
    return action
}