2017-07-17 9 views
0

2-3 TableViewを使用している場合、どのようにして特定のTableViewの「削除」行を無効にすることができますか?文の場合、私はの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() 
     } 
    } 
} 

を働いていない私はsecondTableViewためのviewDidLoadでfalseに編集モードを設定しようとしましたが、それはまた、働いていません。

secondTableView.setEditing(false, animated: false) 

私は、デフォルトではfalseに設定されていますことを理解し、私はcommit editingStyleはすべてtableViewsのためにそれを有効にするので、私は第二のためにそれを無効にすることができればと思いました。

答えて

1

TableViewにタグを付けて、ifまたはswitchのステートメントで確認してください。

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

関連する問題