如何在swift中删除按钮操作上的uitableview行?

如何在swift中删除按钮操作上的uitableview行?,uitableview,swift2,Uitableview,Swift2,我想删除swift中uibutton操作上的UITableView行。您需要做的大致如下: func myButtonMethod(sender : UIButton!) { self.dataSource.removeAtIndex(myIndex) // dataSource being your dataSource array self.tableview!.reloadData() // you can also call this method if you want t

我想删除swift中uibutton操作上的
UITableView
行。

您需要做的大致如下:

func myButtonMethod(sender : UIButton!) {
   self.dataSource.removeAtIndex(myIndex) // dataSource being your dataSource array
   self.tableview!.reloadData()
// you can also call this method if you want to reduce the load, will also allow you to choose an animation  
//self.tableView!.deleteRowsAtIndexPaths([NSIndexPath(forItem: myIndex, inSection: 0)], withRowAnimation: nil)
}
添加如下方法作为按钮的目标:

 self.myButton.addTarget(self, action: "myButtonMethod:", forControlEvents: .TouchUpInside)

您需要做的是:

func myButtonMethod(sender : UIButton!) {
   self.dataSource.removeAtIndex(myIndex) // dataSource being your dataSource array
   self.tableview!.reloadData()
// you can also call this method if you want to reduce the load, will also allow you to choose an animation  
//self.tableView!.deleteRowsAtIndexPaths([NSIndexPath(forItem: myIndex, inSection: 0)], withRowAnimation: nil)
}
添加如下方法作为按钮的目标:

 self.myButton.addTarget(self, action: "myButtonMethod:", forControlEvents: .TouchUpInside)
你可以这样做:

@IBAction func removeCell() {
    self.dataSource.removeAtIndex(index) // this is the dataSource array of your tableView
    tableView.deleteRowsAtIndexPaths([NSIndexPath(forItem: index, inSection: 0)], withRowAnimation: .Left)
}
将此
iAction
连接到按钮的
touchUpInside
操作。确保先从数据源中删除该元素,然后才能从表视图中删除它

如果您不想设置删除行的动画,只需将该行代码替换为
tableView.reloadData()

即可:

@IBAction func removeCell() {
    self.dataSource.removeAtIndex(index) // this is the dataSource array of your tableView
    tableView.deleteRowsAtIndexPaths([NSIndexPath(forItem: index, inSection: 0)], withRowAnimation: .Left)
}
将此
iAction
连接到按钮的
touchUpInside
操作。确保先从数据源中删除该元素,然后才能从表视图中删除它

如果您不想设置删除行的动画,只需将该行代码替换为
tableView.reloadData()

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
   rowIndex = indexPath.row          // declare a global variable
}

@IBAction func reomveButton(_ sender: Any) 
{
   self.array.remove(at: rowIndex)
   TableView.reloadData()
}
也可以尝试以下方法:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
   rowIndex = indexPath.row          // declare a global variable
}

@IBAction func reomveButton(_ sender: Any) 
{
   self.array.remove(at: rowIndex)
   TableView.reloadData()
}
没什么解释。。。 对于Swift 4,Swift 5。。。 第一个声明类变量:

var indexForCell = Int()
然后执行iAction以删除行:

@IBAction func removeRow(_ sender: Any) { 
    self.modelArray.remove(at: indexForCell)
    myTableView.reloadData()
}
更改索引变量的默认值以匹配indexPath:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    indexForCell = indexPath.row
}
最后,向可重用单元格按钮添加操作:

myRemoveButton.addTarget(self, action: #selector(self.removeRow), for: .touchDown)
这将使用myRemoveButton删除行的每个实例。

小说明。。。 对于Swift 4,Swift 5。。。 第一个声明类变量:

var indexForCell = Int()
然后执行iAction以删除行:

@IBAction func removeRow(_ sender: Any) { 
    self.modelArray.remove(at: indexForCell)
    myTableView.reloadData()
}
更改索引变量的默认值以匹配indexPath:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    indexForCell = indexPath.row
}
最后,向可重用单元格按钮添加操作:

myRemoveButton.addTarget(self, action: #selector(self.removeRow), for: .touchDown)

这将使用myRemoveButton删除行的每个实例。

请提供您的研究和代码片段。我在uitableview中有多行,我想删除按钮操作中的特定行。显示您迄今为止尝试过的代码。请提供您的研究和代码片段。我在uitableview中有多行,我想删除按钮操作上的特定行。显示您迄今为止尝试过的代码。重新加载整个表视图会增加处理开销。调用“deleteRowsAtIndexPaths”就足够了。该按钮存在于UICell类中,但我们如何访问self.myButton?@YashJain您可以为单元格创建自定义setter,并删除UITableViewCell的prepareForReuse方法中的所有内容。请检查这里的代码以获得适当的示例(没有按钮,但它应该可以帮助您):重新加载整个表视图是处理开销。调用“deleteRowsAtIndexPaths”就足够了。该按钮存在于UICell类中,但我们如何访问self.myButton?@YashJain您可以为单元格创建自定义setter,并删除UITableViewCell的prepareForReuse方法中的所有内容。请检查此处的代码以获得适当的示例(没有按钮,但它应该对您有所帮助):