SHARE
TWEET

Swipe Action View Controller

a guest Sep 16th, 2019 131 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  ViewController.swift
  3. //  SampleSwipe
  4. //
  5.  
  6. import UIKit
  7.  
  8. class ViewController: UIViewController {
  9.  
  10.   private var dataSource: [String] = [
  11.     "One",
  12.     "Two",
  13.     "Three",
  14.     "Four",
  15.     "Five",
  16.     "Six"
  17.   ]
  18.  
  19.   @IBOutlet weak var tableView: UITableView!
  20.  
  21.   override func viewDidLoad() {
  22.     super.viewDidLoad()
  23.     tableView.dataSource = self
  24.     tableView.delegate = self
  25.   }
  26.  
  27.  
  28. }
  29.  
  30. extension ViewController: UITableViewDelegate {
  31.  
  32.   func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
  33.     let deleteAction = UIContextualAction(style: .destructive, title: "Delete") { [weak self] _, _, completion in
  34.       self?.dataSource.remove(at: indexPath.row)
  35.       completion(true)
  36.       tableView.reloadData()
  37.     }
  38.     return UISwipeActionsConfiguration(actions: [deleteAction])
  39.   }
  40.  
  41. }
  42.  
  43. extension ViewController: UITableViewDataSource {
  44.  
  45.   func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  46.     return dataSource.count
  47.   }
  48.  
  49.   func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  50.     let cell = tableView.dequeueReusableCell(withIdentifier: "testCell", for: indexPath)
  51.     cell.textLabel?.text = dataSource[indexPath.row]
  52.     cell.detailTextLabel?.text = "\(indexPath.row)"
  53.     return cell
  54.   }
  55.  
  56.  
  57. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top