【问题标题】:delete a row in table view in swift快速删除表格视图中的一行
【发布时间】:2015-03-27 05:46:50
【问题描述】:

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

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)
     }
}

【问题讨论】:

  • 我想你检查函数语法
  • func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
  • @LeonardoSavioDabus 我使用了与你建议的相同的语法,我也更新了我的问题,但删除按钮仍然没有出现。
  • 我发现@Nilesh 下面的答案有效。我错过了查看删除按钮的约束。

标签: ios uitableview swift


【解决方案1】:

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

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

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()
   }
}

你必须刷新表格。

【讨论】:

  • tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) 比重新加载整个表格更好
  • 读起来很痛苦 reloadData()。正如丹尼所说,使用“deleteRowAtIndexPaths”
【解决方案2】:
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()

        }

【讨论】:

  • beginUpdate() 和 endUpdate() 解决了我在 Swift 3 中尝试调用 DeleteRow 时应用程序崩溃的问题
【解决方案3】:

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

【讨论】:

  • 我有(一些)限制,但显然还不够。自动布局是一门艺术,而不是一门科学。 :)
【解决方案4】:

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

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

注意:'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]
}

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

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]
}

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

【讨论】:

  • commit edittingStyle 在 Swift 3 中为我工作.. 在他们的页面上发布了一个工作示例.. 你的方法也对我有用。看起来好多了。
【解决方案5】:
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)
}
}

【讨论】:

  • 我使用的是表格视图而不是表格视图控制器,我无法使用覆盖,我已经以相同的方式实现了该方法,但它不起作用。
【解决方案6】:

为斯威夫特 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
    }
}

【讨论】:

    【解决方案7】:

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

    【讨论】:

      【解决方案8】:
      func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
                      if editingStyle == UITableViewCellEditingStyle.Delete { 
         yourArray.removeAtIndex(indexPath.row)
      
         tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
      
          }
      
       }
      

      【讨论】:

        【解决方案9】:
        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()   
         }
         }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-06-03
          • 2017-05-19
          • 1970-01-01
          • 1970-01-01
          • 2016-11-04
          • 2018-06-06
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多