Add swipe to delete UITableViewCell

后端 未结 25 1638
暖寄归人
暖寄归人 2020-11-30 17:50

I am making a CheckList application with a UITableView. I was wondering how to add a swipe to delete a UITableViewCell.

This is my ViewCont

25条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-30 18:33

    For > ios 13 https://gist.github.com/andreconghau/de574bdbb468e001c404a7270017bef5#file-swipe_to_action_ios13-swift

    /*
         SWIPE to Action
         */
        
        func tableView(_ tableView: UITableView,
                       editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
            return .none
        }
        // Right Swipe
        func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
            
            let action = UIContextualAction(style: .normal,
                                            title: "Favourite") { [weak self] (action, view, completionHandler) in
                                                self?.handleMarkAsFavourite()
                                                completionHandler(true)
            }
            action.backgroundColor = .systemBlue
            
            return UISwipeActionsConfiguration(actions: [action])
        }
        
        func tableView(_ tableView: UITableView,
                           trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
            // Archive action
            let archive = UIContextualAction(style: .normal,
                                             title: "Archive") { [weak self] (action, view, completionHandler) in
                                                self?.handleMoveToArchive()
                                                completionHandler(true)
            }
            archive.backgroundColor = .systemGreen
    
            // Trash action
            let trash = UIContextualAction(style: .destructive,
                                           title: "Trash") { [weak self] (action, view, completionHandler) in
                self?.handleMoveToTrash(book: (self?.books![indexPath.row]) as! BookItem)
                                            completionHandler(true)
            }
            trash.backgroundColor = .systemRed
    
            // Unread action
            let unread = UIContextualAction(style: .normal,
                                           title: "Mark as Unread") { [weak self] (action, view, completionHandler) in
                                            self?.handleMarkAsUnread()
                                            completionHandler(true)
            }
            unread.backgroundColor = .systemOrange
    
            let configuration = UISwipeActionsConfiguration(actions: [trash, archive, unread])
            // If you do not want an action to run with a full swipe
            configuration.performsFirstActionWithFullSwipe = false
            return configuration
        }
        
        
        
        private func handleMarkAsFavourite() {
            print("Marked as favourite")
        }
    
        private func handleMarkAsUnread() {
            print("Marked as unread")
        }
    
        private func handleMoveToTrash(book: BookItem) {
            print("Moved to trash")
            print(book)
            let alert = UIAlertController(title: "Hi!", message: "Bạn có muốn xóa \(book.name)", preferredStyle: .alert)
                
                 let ok = UIAlertAction(title: "Xóa", style: .default, handler: { action in
                    book.delete()
                    self.listBook.reloadData()
                 })
                 alert.addAction(ok)
                 let cancel = UIAlertAction(title: "Hủy", style: .default, handler: { action in
                 })
                 alert.addAction(cancel)
                 DispatchQueue.main.async(execute: {
                    self.present(alert, animated: true)
            })
            
        }
    
        private func handleMoveToArchive() {
            print("Moved to archive")
        }
    

提交回复
热议问题