tableView Cell 왼쪽으로 슬라이드시켜 삭제 버튼 등을 표시

14103 단어 Swift

이번 내용





기능 설명



tableView의 셀을 왼쪽으로 슬라이드했을 때 셀을 삭제하는 버튼과 셀을 늘리는 버튼 표시

코드와 간략한 설명


  • 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])
        }
    
    }
    



    지적, 질문 등 있으면, 코멘트까지 부탁드립니다.

    좋은 웹페이지 즐겨찾기