简体   繁体   中英

How to swipe to delete in tableview editing mode? (swift)

I want to make table view can and reorder.

It has hamburger button to reorder the cells and I can swipe the cell to delete the cell like iOS music app.

So I make this way and I can't swipe the cell. I think the tableView.setEditing makes cell can't swipe.

How can I make it?

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return true
}


func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    
    let action = UIContextualAction(style: .normal, title: nil) { (action, view, completion) in
        self.shoppingList.remove(at: indexPath.row)
        tableView.deleteRows(at: [indexPath], with: .fade)
        completion(true)
    }
    
    action.backgroundColor = .white
    action.image = UIImage(named: "delete")
    
    let configuration = UISwipeActionsConfiguration(actions: [action])
    configuration.performsFirstActionWithFullSwipe = false
    return configuration
    
}


func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
    return true
}

func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
    return .none
}

func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
    return false
}


func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
    let movedCell = self.shoppingList[sourceIndexPath.row]
    shoppingList.remove(at: sourceIndexPath.row)
    shoppingList.insert(movedCell, at: destinationIndexPath.row)
}


override func viewDidLoad() {
    super.viewDidLoad()

    setup()
    bindConstraints()
}


func setup() {
   
    let footerView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 39))
    footerView.addSubview(writeButton)
    
    tableView.delegate = self
    tableView.dataSource = self
    tableView.tableFooterView = footerView
    //tableView.sectionFooterHeight = 100
   
    self.view.addSubview(tableView)
    
    tableView.setEditing(true, animated: true)
}

Try with

func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
    return .delete
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM