简体   繁体   English

如何从自定义单元格中删除表视图行?

[英]How to delete table view row from custom cell?

I have a custom table view cell with two buttons. 我有一个带有两个按钮的自定义表格视图单元格。 If either one is pressed, I want the table view to delete the row. 如果任何一个被按下,我希望表格视图删除该行。 I already know the code for that: 我已经知道该代码:

myArray.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)

I just would like to call this from my custom cell. 我只是想从我的自定义单元格中调用它。 I have it set up like so: 我有这样设置:

@IBAction func handleDecline(sender: AnyObject)
{
    // Remove table view row 
}

@IBAction func handleApprove(sender: AnyObject)
{
    // Remove table view row
}

How would I go about doing that? 我将如何去做?

You should not handle deletion of a cell from the cell itself. 您不应处理从单元格本身删除单元格的问题。 It violates MVC pattern. 它违反了MVC模式。 Events should be delegated to the view controller and handled there with deleteRows method like this: 应该将事件委托给视图控制器,并使用deleteRows方法在其中进行处理,如下所示:

tableView.deleteRows(at: [indexPath], with: .fade)

Also tableView's dataSource must be updated accordingly, otherwise app will crash. 还必须相应地更新tableView的dataSource,否则应用程序将崩溃。

Here you can find an example of how you can delegate an event from cell to view controller 在这里,您可以找到一个示例,该示例说明了如何从单元委派事件到视图控制器

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

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