2016-12-01 10 views
1

私はコレクションビューを使用してデータの配列を取得するアプリケーションを持っています。コレクションビューのセルのようなボタンを実装

私は、ストーリーボード経由でコレクションビューのセルにボタンを追加しました。ユーザーがボタンをクリックすると、選択した状態が変化します。どちらがうまくいきますか。

次に、NSUserDefaultsを使用してキー "isSelected"のUIButtonのbool値を保存して取得しようとしました。

これは、NSUserDefaultを使用してbool値を変更して保存する方法です。

@IBAction func likeBtn(_ sender: UIButton) { 

if sender.isSelected == false { 


      sender.isSelected = true 

      let defaults = UserDefaults.standard 

      defaults.set(true, forKey: "isSelected") 


     } 

     else { 


      sender.isSelected = false 

      let defaults = UserDefaults.standard 

      defaults.set(false, forKey: "isSelected") 


     } 
} 

そして、私のcellForItemAtのindexPathで私はUIButtonのためのbool値を取得しようとした。..

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = newsfeedColView.dequeueReusableCell(withReuseIdentifier: "NewsFeed", for: indexPath) as! NewsFeedCollectionViewCell 

     cell.likeBtn.addTarget(self, action: #selector(NewsFeedViewController.likeBtn(_:)), for: UIControlEvents.touchUpInside) 


     var defaults = UserDefaults.standard 

     var state = defaults.bool(forKey: "isSelected") 

     cell.likeBtn.isSelected = state 

return cell 

} 

、私はアプリを使用していますまで、すべてが正常に動作しますが、私は再びアプリやオープンを終了すると保存されたブール値は、以前にアプリを使用して選択したセルではなく、各セルのUIButtonに割り当てられます。

NSUserDefaultを使用して保存する際にインデックスパスを指定する必要があると思います。しかし、私は迅速かつXcodeの新機能としてどのように把握することはできません..どのように先に進むように助けてください。

Screen Shot of the viewcontroller

すべてのヘルプ...関係者は長年のためにここで吸う...このような状況に対処する方法を考え出すカント...してください...

+0

1)これを達成するためにユーザーデフォルトを行う必要はありません。 'likeBtn.isSelected'を使って' cellForItemAt'内のボタンを参照することができます。2)userdefaultsに 'index.row'を格納する必要があります。3)ボタンはviewControllerのビューに関連しています。細胞。 – Honey

+0

私は申し訳ありませんが、私はあなたが暗示しようとしているものを得られませんでしたか? –

+0

どの部分? 1,2,3? – Honey

答えて

0

あなただけのすべてのための一つの値を使用しているとあなたが選択したときのtableViewの行は真であり、今起こっているすべてのものに当てはまります。 実際には、データベースごとに「選択された」状態を保存する必要があります。 これは単なるプロトタイプであれば、UserDefaultsをクイックDBとして使用できます。これを行うには、それぞれの 'isSelected'行に異なるキーを使用し、ボタンのタグをキーとして使用する必要があります。

@IBAction func likeBtn(_ sender: UIButton) 
{ 
    if sender.isSelected == false { 
    sender.isSelected = true 

    let defaults = UserDefaults.standard 
    defaults.set(true, forKey: "isSelected\(sender.tag)") 
    } 
    else { 
    sender.isSelected = false 
    let defaults = UserDefaults.standard 
    defaults.set(false, forKey: "isSelected\(sender.tag)") 
    } 
} 

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = newsfeedColView.dequeueReusableCell(withReuseIdentifier: "NewsFeed", for: indexPath) as! NewsFeedCollectionViewCell 

    cell.likeBtn.addTarget(self, action: #selector(NewsFeedViewController.likeBtn(_:)), for: UIControlEvents.touchUpInside) 

    let tag = indexPath.row 
    cell.likeBtn.tag = tag 
    var defaults = UserDefaults.standard 
    var state = defaults.bool(forKey: "isSelected\(tag)") 

    cell.likeBtn.isSelected = state 

    return cell 
} 
+0

私はこの解決策を試しましたが、セルが消えたり、アプリケーションを再起動したりすると、ボタンの選択された状態がデフォルトの状態(false)にリセットされます。私は間違って何をしているのか理解していない。このような問題に対処するための正しいアプローチか、別のアプローチを使用するべきです。 –

+0

これを達成できますか?この問題を解決する方法はありません。 –

関連する問題