0

I have made a favorite button for my detailViews( i have a master-detail app ) and it saves the button state generally for all cells/DetailsViews...I want if i press index's 3 cell it will save the button state only for there , if i go to index 4 it will save it individually to that row and won't save the same state to all cells.

Favorite Button:

//create a new button
        let Favoritebutton: UIButton = UIButton(type: UIButtonType.custom)
        //set image for button
        Favoritebutton.setImage(UIImage(named: "EmptyHeart.png"), for: .normal)
        Favoritebutton.setImage(UIImage(named: "FilledHeart.png"), for: .selected)
        //add function for button
        Favoritebutton.addTarget(self, action: #selector(self.button), for: .touchUpInside)
        //set frame
        Favoritebutton.frame = CGRect(x:0,y: 0,width: 35,height: 35)
        
        Favoritebutton.isSelected = UserDefaults.standard.bool(forKey: "isSaved")
        
        let barButton = UIBarButtonItem(customView: Favoritebutton)
        //assign button to navigationbar
        
        self.navigationItem.rightBarButtonItem = barButton
        
func button(sender: UIButton) {
        
        audioPlayer.play()
        
        let newValue = !sender.isSelected
        sender.isSelected = newValue
        UserDefaults.standard.set(newValue, forKey: "isSaved")
        
        let tabItem = self.tabBarController?.tabBar.items![3]
        sel_val = tabItem?.badgeValue
        if(sel_val == nil){
            sel_val = "0"
        }
        let sel_num  = Int(sel_val!)
        
        let fav: NSMutableArray = []
        fav.add(barImage)
        fav.add(barName)
        fav.add(streetName)
        if sender.isSelected {
            tabItem!.badgeValue = String(format: "%d", sel_num! + 1)
            favorite.add(fav)
        } else {
            tabItem!.badgeValue = String(format: "%d", sel_num! - 1)
            favorite.remove(fav)
        }
    }

How can i make the button save state for each indexPath individually like i want?

This will help me finish my favorites feature so your help will be really really appreciated !

Thank you for your help !

Community
  • 1
  • 1
Newbie Questions
  • 463
  • 1
  • 11
  • 31

1 Answers1

0

What you want is to store array or dictionary, depends on the situation. Here is an example how to store an array How to save NSMutablearray in NSUserDefaults. I suggest to get the values from defaults when app has started and assign it to the value, so you have a local copy. Do not forget to save array back to defaults after every change, or at least when leaving the app.


EDIT:

You want to store Dictionary of bool, so that you can access values for every row. I discourage you to use indexPath, rather you should use an identifier which will be unique for a row.

These will serve as local storage for your app

let favoritesKey = "favorites"
var favorites: [Int: Bool] = [:]

This is how you obtain saved dictionary:

favorites = userDefaults.object(forKey: favoritesKey) as? [Int : Bool] ?? [:]

This is how you change your values:

favorites[index] = true / false

This is how you obtain your values:

let value = favorites[index]

This is how you save values:

let userDefaults = UserDefaults.standard
userDefaults.set(favorites, forKey: favoritesKey)
Community
  • 1
  • 1
Luzo
  • 1,336
  • 10
  • 15
  • How can i do all of that? can you help me with a code snippet please? – Newbie Questions Apr 29 '17 at 15:03
  • added snippets for every action, now it's your move to try to implement that, and we can help you later when you are stuck somewhere – Luzo Apr 29 '17 at 15:55
  • Nice! Thank you. 1 last thing , do i put all of this in the button's action? – Newbie Questions Apr 29 '17 at 16:07
  • No definitely not, only things in button's action should be these `favorites[index] = true / false` and let `userDefaults.set(favorites, forKey: favoritesKey)`, storing your local copy might be done in viewDidLoad, obtaining values should obviously happen when you create the button(where you do it now). Do not forget to mark the answer as correct. – Luzo Apr 29 '17 at 17:32
  • man , sorry for being a noob but i can't make this work ...plus i get an error saying : `Cannot subscript a value of type '[Int : Bool]' with an index of type '(IndexPath).Type' (aka 'IndexPath.Type')` trying to change index cause its saying unknown thing such as index – Newbie Questions Apr 29 '17 at 17:51
  • Change everywhere where it says [Int : Bool] to type [IndexPath : Bool] , I think that is more than obvious. Did you even tried to think about what are you typing? – Luzo Apr 29 '17 at 18:03