0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

tableView Cell 左にスライドさせて削除ボタンなどを表示

Posted at

今回の内容

EF5545C3-88E1-40AE-AC48-451AF0DC0967_1_201_a.jpeg

機能説明

tableViewのCellを左にスライドした時に、cellを削除するボタンとcellを増やすボタン表示

コードと簡単解説

  • func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {}を使用します。

  • UIContextualActionでは.image.backgroundColorを使うことで好きな見た目にすることもできます。

tableView
  func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
        
        let deleteAction = UIContextualAction(style: .destructive, title: "削除") { _,_,_  in
                
            self.cellContentsArray.remove(at: indexPath.row)
            self.tableView.deleteRows(at: [indexPath as IndexPath], with: .automatic)
            self.tableView.reloadData()
            
        }
        
        let cellPlus = UIContextualAction(style: .normal, title: "") { _, _, _ in
            
            self.cellContentsArray.insert(String(Int(self.cellContentsArray[indexPath.row])! + 1), at: indexPath.row + 1)
            self.tableView.reloadData()
            
        }
        cellPlus.image = UIImage(systemName: "plus.bubble")
        cellPlus.backgroundColor = .systemGreen
        
        return UISwipeActionsConfiguration(actions: [deleteAction,cellPlus])
    }

全てのコード

ViewController
import UIKit

class ViewController: UIViewController {

    
    @IBOutlet weak var tableView: UITableView!
    
    lazy var cellContentsArray = ["1","2","3","4","5","6","7","8","9","10"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.delegate = self
        tableView.dataSource = self
        
    }
}

extension ViewController:UITableViewDelegate,UITableViewDataSource{
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return cellContentsArray.count
        
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        
        cell.textLabel?.text = cellContentsArray[indexPath.row]
        
        return cell
        
    }
    
    func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
        
        let deleteAction = UIContextualAction(style: .destructive, title: "削除") { _,_,_  in
                
            self.cellContentsArray.remove(at: indexPath.row)
            self.tableView.deleteRows(at: [indexPath as IndexPath], with: .automatic)
            self.tableView.reloadData()
            
        }
        
        let cellPlus = UIContextualAction(style: .normal, title: "") { _, _, _ in
            
            self.cellContentsArray.insert(String(Int(self.cellContentsArray[indexPath.row])! + 1), at: indexPath.row + 1)
            self.tableView.reloadData()
            
        }
        cellPlus.image = UIImage(systemName: "plus.bubble")
        cellPlus.backgroundColor = .systemGreen
        
        return UISwipeActionsConfiguration(actions: [deleteAction,cellPlus])
    }
     
}

終わり

ご指摘、ご質問などありましたら、コメントまでお願い致します。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?