Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ViewController.swift
- // SampleSwipe
- //
- import UIKit
- class ViewController: UIViewController {
- private var dataSource: [String] = [
- "One",
- "Two",
- "Three",
- "Four",
- "Five",
- "Six"
- ]
- @IBOutlet weak var tableView: UITableView!
- override func viewDidLoad() {
- super.viewDidLoad()
- tableView.dataSource = self
- tableView.delegate = self
- }
- }
- extension ViewController: UITableViewDelegate {
- func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
- let deleteAction = UIContextualAction(style: .destructive, title: "Delete") { [weak self] _, _, completion in
- self?.dataSource.remove(at: indexPath.row)
- completion(true)
- tableView.reloadData()
- }
- return UISwipeActionsConfiguration(actions: [deleteAction])
- }
- }
- extension ViewController: UITableViewDataSource {
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return dataSource.count
- }
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cell = tableView.dequeueReusableCell(withIdentifier: "testCell", for: indexPath)
- cell.textLabel?.text = dataSource[indexPath.row]
- cell.detailTextLabel?.text = "\(indexPath.row)"
- return cell
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement