ios 快速删除表格视图中的一行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29294099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 05:22:38  来源:igfitidea点击:

delete a row in table view in swift

iosuitableviewswift

提问by Sunil Kumar

I am trying to delete a row in table view. I have implemented the required methods but when i am swiping the row horizontally no delete button is coming. I have searched and I have found the same solution everywhere but it is not working in my case. I don't know where i am making mistake. Can anyone help me please?

我正在尝试删除表格视图中的一行。我已经实现了所需的方法,但是当我水平滑动行时,没有删除按钮。我已经搜索过并且到处都找到了相同的解决方案,但它在我的情况下不起作用。我不知道我哪里出错了。有人可以帮我吗?

func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool 
{
    return true
}

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) 
{
     if editingStyle == .Delete 
     {
        dataHandler.deletePeripheral(indexPath.row)
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
     }
}

回答by user3182143

If below coding is helpful for you,i will be happy

如果下面的代码对你有帮助,我会很高兴

func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool 
{
    return true
}

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) 
{
     if editingStyle == .Delete 
     {
        yourArray.removeAtIndex(indexPath.row) 
        self.tableView.reloadData()   
     }
}

SWIFT 3.0

斯威夫特 3.0

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool
{
    return true
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
{
   if editingStyle == .delete
   {
      yourArray.remove(at: indexPath.row)
      tblDltRow.reloadData()
   }
}

You have to refresh the table.

您必须刷新表。

回答by Anton

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

        if (editingStyle == UITableViewCellEditingStyle.Delete) {


            self.tableView.beginUpdates()
            self.arrayData.removeObjectAtIndex(indexPath.row) // also remove an array object if exists.
            self.tableView.deleteRowsAtIndexPaths(NSArray(object: NSIndexPath(forRow: indexPath.row, inSection: 2)), withRowAnimation: UITableViewRowAnimation.Left)
            self.tableView.endUpdates()

        }

回答by Nilesh

Apply Autolayout Constraints to table view. The delete button is there but not displaying because of no auto layout

将自动布局约束应用于表格视图。删除按钮在那里但没有显示,因为没有自动布局

回答by amit gupta

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) 
{
if editingStyle == UITableViewCellEditingStyle.Delete {
  numbers.removeAtIndex(indexPath.row)    
  tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
}
}

回答by Geoffrey Waterson

I struggled with this as well but finally found out a solution - I am running XCode 8.3.2. A lot of the answers I read were for older versions of XCode / Swift and many were for Objective-C.

我也为此苦苦挣扎,但最终找到了解决方案 - 我正在运行 XCode 8.3.2。我读到的很多答案都是针对旧版本的 XCode / Swift 的,很多都是针对 Objective-C 的。

The solution I found was to use the 'editActionsForRowAt' tool for UITableViews. Note: Everything else I read kept pointing me towards the 'commit editingStyle' tool for UITableView, but I could never get it to work. Using editActionsForRowAt worked perfectly for me.

我找到的解决方案是对 UITableViews 使用“editActionsForRowAt”工具。注意:我读到的其他所有内容都一直指向UITableView的“ commiteditingStyle”工具,但我永远无法让它工作。使用 editActionsForRowAt 非常适合我。

NOTE: 'postData' is the name of the Array that I added data to.

注意:'postData' 是我添加数据的数组的名称。

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {

    let deleteAction = UITableViewRowAction(style: .default, title: "Delete", handler: { (action, IndexPath) in
        // Remove item from the array
        postData.remove(at: IndexPath.row)

        // Delete the row from the table view
        tableView.deleteRows(at: [IndexPath as IndexPath], with: .fade)
        })
        // set DeleteAction Button to RED (this line is optional - it simply allows you to change the color of the Delete button - remove it if you wish)
        deleteAction.backgroundColor = UIColor.red

    return [deleteAction]
}

I also have some code to add an 'EDIT button' alongside the Delete button:

我还有一些代码可以在删除按钮旁边添加一个“编辑按钮”:

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {

    let editAction = UITableViewRowAction(style: .default, title: "Edit", handler: { (action, IndexPath) in
        //print("Edit tapped")
        })
        // Set EditAction button to blue (this line is not mandatory - it just sets the color of the Edit box to blue)
        editAction.backgroundColor = UIColor.blue

    let deleteAction = UITableViewRowAction(style: .default, title: "Delete", handler: { (action, IndexPath) in
        //print("Delete tapped")
        // Remove item from the array
        postData.remove(at: IndexPath.row)

        // Delete the row from the table view
        tableView.deleteRows(at: [IndexPath as IndexPath], with: .fade)
        })
        // set DeleteAction Button to RED (this line isn't mandatory - it just sets the color of the Delete box)
        deleteAction.backgroundColor = UIColor.green

    return [editAction, deleteAction]
}

EDIT: fixed format so code showed up in the grey box :)

编辑:固定格式,因此代码显示在灰色框中:)

回答by nyxee

for swift 3

快速 3

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == UITableViewCellEditingStyle.delete {
        dataHandler.deletePeripheral(indexPath.row) 
        tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
        // - OR -
        // mTableView.reloadData() //mTableView is an outlet for the tableView
    }
}

回答by user3610878

i know what prob you are having . I think you just check your table constraints they may be wrong just recheck your auto layout constraints

我知道你有什么问题。我想你只是检查你的表格约束他们可能是错的只是重新检查你的自动布局约束

回答by Silambarasan Raman

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
                if editingStyle == UITableViewCellEditingStyle.Delete { 
   yourArray.removeAtIndex(indexPath.row)

   tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)

    }

 }

回答by Sai kumar Reddy

func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool 
{
return true
}

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) 
 {
 if editingStyle == .Delete 
 {
    carsArray.removeAtIndex(indexPath.row) 
    self.tableView.reloadData()   
 }
 }