[英]How to remove a cell from tableView with animation through IBAction? iOS Swift 3.0
我正在构建一个todo-list应用程序,我在视图控制器中持有一个任务模型数组和tableView。
此tableView中的每个单元格都包含多个UI元素,其中一个是UIView,实际上是一个复选框,由Skyscanner的pod实现: https : //github.com/Marxon13/M13Checkbox
当用户点击复选框(带动画)时,我想删除一个单元格。
我为UIView设置了一个IBAction,我知道要删除的数组中的哪个元素(我标记了每个UIView)但我不能使用该方法
tableView.deleteRows(at: [IndexPath], with: UITableViewRowAnimation)
因为我没有索引路径。 我想找到一个很好的方法来删除索引的单元格,最好不要保存indexPath变量(我试过但不知道如何正确实现它,因为可以从各种索引中删除单元格)。
谢谢你的帮助!
感谢所有帮助过的人! 我正在回答我自己的问题,因为这是你的答案的组合!
正如Puneet Sharma,Reinier Melian和Yun CHEN所说,我设法在函数内部创建索引路径。 Puneet的注释非常重要,您必须 在删除单元格之前从数组中删除元素。
PS。 也许我可以在我问问题之前创建IndexPath方式,但是在行中
self.tableView.deleteRows(at: [IndexPath(row: checkbox.tag, section: 0)], with: .automatic)
Xcode根本不会自动完成此初始化。
这是删除单元格的代码,就像我想要的那样:
@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)
}
非常感谢!
在cellForRowatIndexPath
的复选框/按钮上设置动作事件。 不要忘记在每个复选框/按钮上添加标签。
cell.btnCounter.tag = indexPath.row
cell.btnCounter.addTarget(self, action: #selector(self.buttonClicked), for: .touchUpInside)
你需要处理如下事件
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
}
希望这对你有所帮助
//检查此快照代码
@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)
}
我希望它对你有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.