简体   繁体   中英

How to give different tableViews same action between ViewControllers [swift]

I have an app with multiple view controllers with a tableview in each of them. Each view controller's table have their own specific purpose, but I use the same swipe actions in each table. So far I have just been copying and pasting the same code for trailingSwipeActionsConfigurationForRowAt and leadingSwipeActionsConfigurationForRowAt in each table. This feels wrong and I know there should be a way where I can write the code once and use it throughout the app but I am not sure how to do this.

I have tried to extensions of UITableView but can not seem to use the trailingSwipeActionsConfigurationForRowAt or leadingSwipeActionsConfigurationForRowAt functions in the extension file. What am I missing?

Here's the code for trailingSwipeActionsConfigurationForRowAt which is just swipe to delete:

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
        let action = UIContextualAction(style: .destructive, title: "") { (action, view, completionHandler) in

            if let item = self.dataSource.itemIdentifier(for: indexPath) {
                CoreDataManager.sharedManager.deleteItem(item)
            }

            completionHandler(true)
        }

        action.image = UIImage(named: "deleteSymbol")

        let configuration = UISwipeActionsConfiguration(actions: [action])
        return configuration
    }
  • you declare below function as global function for the project and access any where you want
  • You can modify function by adding parameter which you want to use when button clicked. just simply add parameter in function and pass data when you call

    func getSwipeAction(indexpath : IndexPath)-> UISwipeActionsConfiguration{ let action = UIContextualAction(style: .destructive, title: "") { (action, view, completionHandler) in completionHandler(true) } action.image = UIImage(named: "deleteSymbol") let configuration = UISwipeActionsConfiguration(actions: [action]) return configuration }

You can add these inner code in UITableView extension function and try to use it like that. You have to implement trailingSwipeActionsConfigurationForRowAt or leadingSwipeActionsConfigurationForRowAt in each of your view controllers.

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    return self.YourFunctionNameInExtension()
}

You can define a protocol like BaseTableViewProtocol and in this protocol's extension you can write your code. So when you extend your tableView class from this protocol you can use the function. For example,

      protocol BaseTableViewProtocol { } 

      extension BaseTableViewProtocol { 
         func trailingSwipeActionsConfigurationForRowAt() {
            //Something goes here
        }

         func leadingSwipeActionsConfigurationForRowAt() {
            //Something goes here
        }
      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM