简体   繁体   English

如何通过IBAction从动态中删除tableView中的单元格? iOS Swift 3.0

[英]How to remove a cell from tableView with animation through IBAction? iOS Swift 3.0

I'm building a todo-list app, and I'm holding a task model array and tableView in my view controller. 我正在构建一个todo-list应用程序,我在视图控制器中持有一个任务模型数组和tableView。

Each cell in this tableView contains several UI elements, one of them is a UIView that is actually a checkbox, implemented by Skyscanner's pod: https://github.com/Marxon13/M13Checkbox 此tableView中的每个单元格都包含多个UI元素,其中一个是UIView,实际上是一个复选框,由Skyscanner的pod实现: https//github.com/Marxon13/M13Checkbox

I would like to remove a cell when a user tap on the checkbox (with animation). 当用户点击复选框(带动画)时,我想删除一个单元格。

I set an IBAction to the UIView, I know which element in the array I want to remove (I tagged each UIView) but I cannot use the method 我为UIView设置了一个IBAction,我知道要删除的数组中的哪个元素(我标记了每个UIView)但我不能使用该方法

tableView.deleteRows(at: [IndexPath], with: UITableViewRowAnimation)

since I don't have the index path. 因为我没有索引路径。 I want to find a nice way to remove the cell with an index, preferably without holding an indexPath variable (I tried that but not sure how to implement it correctly since cells can be removed from various indexes). 我想找到一个很好的方法来删除索引的单元格,最好不要保存indexPath变量(我试过但不知道如何正确实现它,因为可以从各种索引中删除单元格)。

Thanks for your help! 谢谢你的帮助!

Thanks for everyone that helped! 感谢所有帮助过的人! I'm answering my own question because it was a combination of your answers! 我正在回答我自己的问题,因为这是你的答案的组合!

as Puneet Sharma, Reinier Melian and Yun CHEN said, I managed to create an index path inside the function. 正如Puneet Sharma,Reinier Melian和Yun CHEN所说,我设法在函数内部创建索引路径。 Puneet's comment is very important, you must remove the element from the array before you remove the cell. Puneet的注释非常重要,您必须 删除单元格之前从数组中删除元素。

PS. PS。 Maybe I could create the IndexPath way before I even asked the question, but in the line 也许我可以在我问问题之前创建IndexPath方式,但是在行中

self.tableView.deleteRows(at: [IndexPath(row: checkbox.tag, section: 0)], with: .automatic)

Xcode does not auto complete this initialization at all. Xcode根本不会自动完成此初始化。

This is the code that remove the cell just like I wanted: 这是删除单元格的代码,就像我想要的那样:

       @IBAction func completeBtnPressed(_ sender: Any) {
    let checkbox = (sender as! M13Checkbox)
    self.tasks.remove(at: checkbox.tag)

    self.tableView.deleteRows(at: [IndexPath(row: checkbox.tag, section: 0)], with: .automatic)
}

Thanks a lot! 非常感谢!

Set the action event on the checkbox/Button like bellow in cellForRowatIndexPath . cellForRowatIndexPath的复选框/按钮上设置动作事件。 don't forgot to add the tag on every checkbox/Button. 不要忘记在每个复选框/按钮上添加标签。

cell.btnCounter.tag = indexPath.row
cell.btnCounter.addTarget(self, action: #selector(self.buttonClicked), for: .touchUpInside)

You need to handle event as below 你需要处理如下事件

func buttonClicked(_ sender: UIButton) {
        //Here sender.tag will give you the tapped checkbox/Button index from the cell
        your_array.remove(at: sender.tag) //Remove your element from array
        tableView.deleteRows(at: [IndexPath(row: sender.tag, section: 0))], with: .automatic) //Hope it is in section 0
    }

Hope this help you 希望这对你有所帮助

// Check this snap code //检查此快照代码

@IBAction func  btnAction(_ sender : UIButton){

        // get Indexpath with Button position

         let buttonPosition = sender.convert(CGPoint.zero, to: self.tableview)
         let indexPath: IndexPath? = tableview.indexPathForRow(at: buttonPosition)

        self.yourArrayDatasource.remove(at: indexPath.row)  // don't forget to replace your array of data source

        self.tableview.deleteRows(at: [indexPath!], with: UITableViewRowAnimation.automatic)


    }

I hope it help you 我希望它对你有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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