【问题标题】:Delete row with multiple TableView删除具有多个 TableView 的行
【发布时间】:2017-07-17 19:23:44
【问题描述】:

如果我有 2-3 个 TableView,我怎样才能只为特定的 TableView 禁用“删除”行?当我为 if 语句设置断点以检查使用了哪个 tableView 时,它不起作用

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if tableView == self.firstTableView {
        if editingStyle == .delete {
            array.remove(at: indexPath.row)
            firstTableView.deleteRows(at: [indexPath], with: .fade)
            firstTableView.reloadData()
        }
    }
}

我尝试在 secondTableView 的 viewDidLoad 中将编辑模式设置为 false,但它也不起作用。

secondTableView.setEditing(false, animated: false)

我知道默认情况下它设置为 false,所以我想如果commit editingStyle 为所有 tableViews 启用它,那么我可以先禁用它。

【问题讨论】:

    标签: ios iphone uitableview


    【解决方案1】:

    只需给每个TableView 一个标签,然后在ifswitch 语句中检查它。

    if tableView.tag == 0 {
        if editingStyle == .delete {
            array.remove(at: indexPath.row)
            tableView.deleteRows(at: [indexPath], with: .fade)
            tableView.reloadData()
        }
    }
    

    【讨论】:

    • 我也试过这个。它不工作。无论如何,两个 TableView 都有滑动删除选项。
    • 澄清一下,你想在编辑模式下禁用删除还是在单元格上向左滑动时没有删除按钮?
    • 向左滑动时没有删除按钮。
    • 我会使用editActionsForRowAt 并且只为每个tableView 返回您想要的操作,并使用if 语句检查标签。如果有帮助,我可以用一个例子更新我的答案。
    • 非常感谢,但我已经提供了正确的答案,@Nikhlesh Bagdiya 已经做了同样的事情。
    【解决方案2】:

    正确答案是检查editingStyleForRowAt indexPath中的标签

     func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
        if tableView.tag == 1 {
            return UITableViewCellEditingStyle.delete
        } else {
            return UITableViewCellEditingStyle.none
        }
    
    }
    

    【讨论】:

      【解决方案3】:

      你可以使用:

      func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
      
              // Return false if you do not want the specified item  or table to be editable.
              if tableView == tableVw {
                  return false
              } else {
                  return true
              }
        }
      

      这里的 tableVw 是一个 tableview 对象,您不想编辑它,或者您也可以使用标签而不是对象比较。 然后使用:

      func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
              if editingStyle == .delete {
      
                 //Write your delete cell logic here
                 array.remove(at: indexPath.row)
                 tableView.deleteRows(at: [indexPath], with: .fade)
                 tableView.reloadData()
               }
      }
      

      【讨论】:

      • 非常感谢您的回复。我不确定是否应该将此作为正确答案,因为我已经用相同的信息回答了我的问题。
      • 你必须使用我上面提到的canEditRowAt tableview函数,然后检查它的工作与否
      • 是的,首先实现删除功能是合乎逻辑的。我认为在我的答案中说出来是没有意义的,所以我只是在真正神奇的地方发布了答案。还是谢谢你!
      猜你喜欢
      • 2017-03-04
      • 1970-01-01
      • 1970-01-01
      • 2010-09-06
      • 2013-09-28
      • 2013-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多