UITableViewRowAction vs UISwipeActionsConfiguration

前端 未结 2 533
暗喜
暗喜 2021-01-11 19:35

The two APIs seem to get the same result. In which case is it better to use one over the other?

override func tableView(_ tableView: UITableView, editActions         


        
相关标签:
2条回答
  • 2021-01-11 19:50

    Here is the code which is working for me

    internal func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    
       // let delete = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in
        let contextItem = UIContextualAction(style: .destructive, title: "") {  (contextualAction, view, boolValue) in
        // delete item at indexPath
           //if self.isfilterenabled == true {
             //   return
            //}
    
    
            if self.isfilterenabled == true {
                //entryFilter.filteredEntries[indexPath.row]
                self.entryFilter.filteredEntries.remove(at: indexPath.row)
            } else {
                self.data.remove(at: indexPath.row)
            }
    
            self.table.deleteRows(at: [indexPath], with: .fade)
            self.save()
        }
    
    
    
        //let share = UITableViewRowAction(style: .normal, title: "SavePDF") { (action, indexPath) in
            // share item at indexPath
        let contextItemSave = UIContextualAction(style: .normal, title: "") {  (contextualAction, view, boolValue) in
    
    
    
    
    
            let alert  = UIAlertController(title: "Done! ", message: "PDF has been saved " ,preferredStyle: .alert)
    
            let okAction = UIAlertAction(title: "OK ", style: .default, handler: nil)
            alert.addAction(okAction)
    
            alert.popoverPresentationController?.sourceView = self.view // so that iPads won't crash
    
            // exclude some activity types from the list (optional)
            //activityViewController.excludedActivityTypes = [ UIActivityTypeAirDrop, UIActivityTypePostToFacebook ]
    
            // present the view controller
            self.present(alert, animated: true, completion: nil)
            // present(alert, animated : true, completion : nil )
    
        }
    
       // share.backgroundColor = UIColor.blue
    
        contextItemSave.image = UIImage(named:"PDF.jpg")
        contextItem.image = UIImage(named:"delete.jpg")
        let swipeActions = UISwipeActionsConfiguration(actions: [contextItem,contextItemSave])
    
        return swipeActions
        //return [delete, share]
    }`
    
    0 讨论(0)
  • 2021-01-11 20:14

    It does basically the same, but swipe actions are available since iOS 11 was released and have some new features:

    • You are able to set actions for trailing swipe as well as for leading swipe
    • You can set image of action action.image = UIImage(...). If there is enough space it shows image as well as title

    Also, you should use swipe actions because they are preferred and in the future updates UITableViewRowActions will be deprecated how UITableView header comment can tell us:

    Use -tableView:trailingSwipeActionsConfigurationForRowAtIndexPath: instead of this method, which will be deprecated in a future release.

    0 讨论(0)
提交回复
热议问题