2016-10-18 5 views
1

選択された行を配列に追加する関数を作成しようとしていますが、その行が選択解除されても配列から削除されます。あなたたちはこれで私を助けてくれますか?スウィフト3.0でdidDelectlectRowをdidSelectRowに追加して削除する方法

選択行関数:

var exercises = [exercise]() // Array that will be filled by selected rows 

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    let selectedCell = tableView.cellForRow(at: indexPath) as? exerciseCell 
    let exercisesSelected = selectedCell?.exerciseNameLbl.text 
    exercisenames.insert(exercisesSelected!, at: 0) 
} 

は、これは私の選択解除デリゲートメソッドです:任意の助けのために楽しみにしてい

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { 
     let selectedCell = tableView.cellForRow(at: indexPath) as? exerciseCell 

     if exercisenames.count < 1 { 
      print("empty array") 
     }else{ 
     exercisenames.remove(at: indexPath.row) 
     print(exercisenames) 
     } 
    } 

、ありがとうございました!

ケビン。

+0

別のデリゲートメソッド' deselectRowAtIndexPathがあります。そこから配列を削除することができます。 –

+0

あなたが求めていることは本当にはっきりしません...通常、データの配列(セルで表現するデータ)にオブジェクトを追加/削除します。では、アクションに基づいてオブジェクトを配列から別のオブジェクトに移動するように求めていますか?その場合、あなたは@ New16の答えを使うことができますが、あなたのコードは間違っています。 – Zeb

+1

実際には、そのことから、選択解除の対象を取り除きたいということは明らかです。だから、その代理人はそれを処理する必要があります。 –

答えて

2

for inループを使用して、アレイ内の特定のオブジェクトを削除できます。

for (index,value) in exercisenames.enumerate() { 
    if value == selectedCell.exerciseNameLbl.text { 
     exercisenames.remove(at:index) 
    } 
} 
+0

これは私が探していたものです!偉大なコーダー、ありがとう –

+0

@KevinVugts問題はありません:)私は慎重にスウィフトガイドを読むことをお勧めします、それは多くの助けになります! –

0

uがこのように、それのための唯一のデリゲートfuncを使用することができます: `:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    if exercisenames.count < 1 { 
     print("empty array") 
    } else { 

     //check if exercise exist in array 
     if exercisenames.contain(/*your exercise*/) == true { 
      exercisenames.remove(at: indexPath.row) 
      tableView.deselectRow(at: indexPath, animated: true) 
    } else { 
     //insert to array 
    } 
    } 

} 

メガバイトこれはuのに役立ちます)

関連する問題