2017-07-17 92 views
0

如果我有2-3个TableView,我怎么才能禁用'删除'只为特定的TableView行?当我设置if语句来检查其的tableView使用断点,它不工作删除有多个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() 
     } 
    } 
} 

我试图设置编辑模式为false viewDidLoad中的secondTableView但它也没有工作。

secondTableView.setEditing(false, animated: false) 

据我所知,在默认情况下它被设置为false,所以我想如果commit editingStyle启用所有tableViews,这样我就可以禁用它第二。

回答

1

只要给每个TableView一个标签,并在ifswitch声明中检查它。

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

我也试过这个。它不工作。无论如何,两个TableView都有滑动删除选项。 – Sargot

+0

只是为了澄清,你想在编辑模式下禁用删除或在单元格上滑动时没有删除按钮? – nighttalker

+0

向左滑动时没有删除按钮。 – Sargot

0

正确答案是editingStyleForRowAt indexPath

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

} 
0

检查标签,您可以使用:

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

非常感谢您的回复。我不确定是否应该将此作为正确答案,因为我已经用相同的信息回答了我的问题。 – Sargot

+0

你必须使用canEditRowAt tableview函数,如上所述,然后检查它的工作与否 –

+0

是的,这是合乎逻辑的首先实现删除功能。我认为在我的回答中没有意义,所以我只是在真正的魔法发生的地方发布了答案。不管怎样,谢谢你! – Sargot