Table Swipe Actions in Swift 4 using Xcode 9.2 - iOS11

Edit actions to tableview rows has been possible since iOS 8 only right trailing side. The new style swipe actions added in iOS 11 and it used by apple in mail and other apps.


In this tutorial, You can learn how to adding swipe actions to a tableView support iOS11 and swift 4.

Before the swipe actions implementation, you study the tutorial of UITableViewController. Download the code from the post.


Trailing Swipe (Swipe To Left)
@available(iOS 11.0, *)
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let delete = UIContextualAction(style: .destructive, title: "Delete") { (action, sourceView, completionHandler) in
        print("index path of delete: \(indexPath)")
        completionHandler(true)
    }

    let rename = UIContextualAction(style: .normal, title: "Edit") { (action, sourceView, completionHandler) in
        print("index path of edit: \(indexPath)")
        completionHandler(true)
    }
    let swipeActionConfig = UISwipeActionsConfiguration(actions: [rename, delete])
    swipeActionConfig.performsFirstActionWithFullSwipe = false
    return swipeActionConfig
}


Leading Swipe (Swipe To Right)
@available(iOS 11.0, *)
    override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
        let makeRead = UIContextualAction(style: .normal, title: "Read") { (action, sourceView, completionHandler) in
            print("index path of edit: \(indexPath)")
            completionHandler(true)
        }
        let swipeActionConfig = UISwipeActionsConfiguration(actions: [makeRead])
        swipeActionConfig.performsFirstActionWithFullSwipe = false
        return swipeActionConfig
    }



Thanks.

Table Swipe Actions in Swift 4 using Xcode 9.2 - iOS11 Table Swipe Actions in Swift 4 using Xcode 9.2 - iOS11 Reviewed by KIRIT MODI on 09:10:00 Rating: 5

No comments:

Powered by Blogger.