简体   繁体   English

删除具有多个TableView的行

[英]Delete row with multiple TableView

If I have 2-3 TableView, how can I disable 'Delete' row only for the specific TableView? 如果我有2-3个TableView,如何仅对特定的TableView禁用“删除”行? When I set breakpoint for if statement to check which tableView is used, it's not working 当我为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()
        }
    }
}

I tried to set editing mode to false in viewDidLoad for secondTableView but it's not working also. 我试图将secondTableView的viewDidLoad中的编辑模式设置为false,但是它也无法正常工作。

secondTableView.setEditing(false, animated: false)

I understand that by default it's set to false, so I thought if commit editingStyle enable it for all tableViews, so I can disable it for second. 我知道默认情况下它设置为false,所以我认为如果commit editingStyle对所有tableViews都启用了它,那么我可以将其禁用一秒钟。

Just give each TableView a tag and check it in an if or switch statement. 只需给每个TableView一个标签,然后在ifswitch语句中对其进行检查即可。

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

The correct answer is checking for tag in editingStyleForRowAt indexPath 正确的答案是在editingStyleForRowAt indexPath检查标记

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

}

You can use: 您可以使用:

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

Here tableVw is a tableview object which you want to not editable or you can also use tag instead of object compare. 这里tableVw是一个您希望不可编辑的tableview对象,或者您也可以使用标签代替对象比​​较。 Then use: 然后使用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM