简体   繁体   中英

Swift 5 how to delete and copy tableview cell by using button

how to delete and copy tableview cell by using button

var dataAry:[Section] = Section.modelArray()

func tableView(_ tableView: UITableView, willSelectRowAt indexPath: 
IndexPath) -> IndexPath? {

    if let indexPathForSelectedRow = tableView.indexPathForSelectedRow,
        indexPathForSelectedRow == indexPath {
        tableView.deselectRow(at: indexPath, animated: false) 
        view.isHidden = true

        return nil
    }
    view.isHidden = false

    return indexPath
    }


@IBAction func didTapDeleteBtn(_ sender: Any) {
   print("didTapDeleteBtn")

   let indexPath = IndexPath(item: 0, section: 0)
   "tableviewcell".remove(at: 0)
   tableView.deleteRows(at: [indexPath], with: .fade)
   tableView.reloadData()

}
@IBAction func didTapCopyBtn(_ sender: Any) {
        print("didTapCopyBtn")
}

button will show inside the view

this only deleted the first row of the tableviewcell

It deletes the first row in the first section in because you told the function to delete such an indexPath indexPath = IndexPath(item: 0, section: 0)

Second you need to save which indexPath to copy/delete when an indexPath is selected In your view controller have a variable:

var SelectedIndexPath = IndexPath
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
     SelectedIndexPath = indexPath
}

@IBAction func didTapDeleteBtn(_ sender: Any) {
    print("didTapDeleteBtn")
    YOUR_MODEL.remove(at: SelectedIndexPath.row)// if your model is just an array
    tableView.deleteRows(at: [SelectedIndexPath], with: .fade)
    //tableView.reloadData()// You normally do not need reloadData() if you use tableView.deleteRows

}

@IBAction func didTapCopyBtn(_ sender: Any) {
    print("didTapCopyBtn")
    dataAry.append[dataAry[SelectedIndexPath]]
    tableView.reloadRows(at: SelectedIndexPath, with: .none)

}

You need to know the index path of the cell tapped to be able to infer which cell to append under or delete.

The best way to do this is to handle the button press in the cell, then call a delegate which you set as the view controller in your cellForRowAtIndexPath method. See here: Remove cell when button pressed inside cell CustomTableViewCell

Here's some sample code. Also notice you don't need to call reloadData() if you tell your table view you're updating it and you're finished updating it. This will be significantly more performant, as you do not need to reload every cell to update one.


// Inside your cell's class
@IBAction func didTapDeleteBtn(_ sender: Any) {
    self.delegate?.deleteButtonWasTappedIn(cell: self)
}

// Inside your view controller
func deleteButtonWasTappedIn(cell: YourCustomCellClass) {
    tableView.beginUpdates()
    tableView.deleteRows(at: [self.tableView.indexPath(for: cell)], with: .fade)
    tableView.endUpdates()
}

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