ios - trying to create Favorite Button for add items in favorite list -


i'm trying create favorite button on collectionviewcell saving items in favorite list. when tapped on favorite icon button add selected cell item in array of favorite list class, , shows in favorite list. when tapped on favorite button change image according values saved or not. when i'm trying save favorite list array in nsuserdefaults save , show values when retrieve it. when close app simulator , run again not shows values in favorite list array saved in nsuserdefaults , want set button image state according item add or not.

output when tapped on favorite. , run again after closing app. can tell me how can fix that. or can please fix in project. thanks

import uikit  var url : [nsstring] = [     "https://www.youtube.com/watch?v=9h30bx4klxg",     "https://www.youtube.com/watch?v=ij_0p_6qtss",     "https://www.youtube.com/watch?v=ajtdxiazrmo",     "https://www.youtube.com/watch?v=h202k7kfzl0",     "https://www.youtube.com/watch?v=cgyed0akwze",     "https://www.youtube.com/watch?v=atkzkl7bgu0",     "https://www.youtube.com/watch?v=4sylli5djz0"]  var image : [nsstring] = ["1.jpg", "2.jpg", "3.jpg", "4.jpg", "5.jpg", "6.jpg", "7.jpg"]   class livecollectionview: uiviewcontroller, uicollectionviewdatasource,uiwebviewdelegate {      @iboutlet weak var collectionview: uicollectionview!      override func viewdidload() {         super.viewdidload()     }      override func viewdidappear(animated: bool) {         let value = uiinterfaceorientation.portrait.rawvalue         uidevice.currentdevice().setvalue(value, forkey: "orientation")     }      func collectionview(collectionview: uicollectionview, numberofitemsinsection section: int) -> int {         return url.count     }      func collectionview(collectionview: uicollectionview, cellforitematindexpath indexpath: nsindexpath) -> uicollectionviewcell     {         let cell = collectionview.dequeuereusablecellwithreuseidentifier("cell", forindexpath: indexpath) as! livecollectionviewcell          cell.imageview.image = uiimage(named: image[indexpath.row] string)         cell.playbt.addtarget(self, action: selector("webview:"), forcontrolevents: uicontrolevents.touchupinside)          cell.playbt.tag = indexpath.row         cell.heartbt.tag = indexpath.row          return cell     }      func collectionview(collectionview: uicollectionview, didselectitematindexpath indexpath: nsindexpath){     } } 

import uikit  class livecollectionviewcell: uicollectionviewcell{  var showplaybutton = true  var number = int() var secondnumber = int()  @iboutlet weak var playbt: uibutton!  @iboutlet weak var imageview: uiimageview!  @iboutlet weak var heartbt: uibutton!  @ibaction func heartbt(sender: uibutton) {       var btn : nsinteger     btn = sender.tag nsinteger      let uvalues = url[btn]     let ivalues = image[btn]      if showplaybutton == true{         u.append(url[btn])         i.append(image[btn])          self.heartbt.setimage(uiimage(named: "rheart"), forstate: uicontrolstate.normal)         showplaybutton = false         print("rheart xxxxxxxxxxxxxxx")      }else{          check in u {              number++             print(number)              if uvalues == check{                 print(u)                 u.removeatindex(number-1)                 number = 0                 print("remove @ indext path")                 print(u)                 break              }          }          check2 in i{              secondnumber++             print(secondnumber)              if ivalues == check2{                 print(i)                 i.removeatindex(secondnumber-1)                 secondnumber = 0                 print("remove @ indext path")                 print(i)                 self.heartbt.setimage(uiimage(named: "wheart"), forstate: uicontrolstate.normal)                 showplaybutton = true                 print("wheart ........")                 break              }          }      }  } } 

import uikit var u : [nsstring] = [] var : [nsstring] = [] class favoritetvviewcontroller: uiviewcontroller , uicollectionviewdelegate{     var uv : nsarray = []     var iv : nsarray = []      @iboutlet weak var collectionview: uicollectionview!      let reuseidentifier = "cell"      override func viewdidload(){     }      override func viewwillappear(animated: bool) {         collectionview.reloaddata()         nsuserdefaults.standarduserdefaults().setobject(u, forkey: "u")         nsuserdefaults.standarduserdefaults().setobject(i, forkey: "i")         nsuserdefaults.standarduserdefaults().synchronize()          uv = nsuserdefaults.standarduserdefaults().arrayforkey("u") as! nsarray         iv = nsuserdefaults.standarduserdefaults().arrayforkey("i") as! nsarray         print("uv values.. \(uv)")         print("iv values.. \(iv)")     }      func collectionview(collectionview: uicollectionview, numberofitemsinsection section: int) -> int     {         return uv.count     }      func collectionview(collectionview: uicollectionview, cellforitematindexpath indexpath: nsindexpath) -> favoritetvcollectionviewcell     {         let cell = collectionview.dequeuereusablecellwithreuseidentifier(reuseidentifier, forindexpath: indexpath) as! favoritetvcollectionviewcell          cell.imageview.image = uiimage(named: iv[indexpath.row] as! string)         return cell     } } 

import uikit class favoritetvcollectionviewcell: uicollectionviewcell {     @iboutlet weak var imageview: uiimageview! } 

i had checked sample project,i made small change it's working fine.

override func viewwillappear(animated: bool) {     collectionview.reloaddata()     //you have check availablity of favorute item before adding userdefault      if u.count > 0 {         nsuserdefaults.standarduserdefaults().setobject(u, forkey: "u")         nsuserdefaults.standarduserdefaults().setobject(i, forkey: "i")         nsuserdefaults.standarduserdefaults().synchronize()     }       uv = nsuserdefaults.standarduserdefaults().arrayforkey("u")!     iv = nsuserdefaults.standarduserdefaults().arrayforkey("i")!     print("uv values.. \(uv)")     print("iv values.. \(iv)") } 

check availability of object before adding userdefault.now working fine give try , let me know response pls.

edit: have edited code working fine,i hear list out changes done me.

  1. in livecollectionview.swift:

    declaring 2 objects , in viewdidload:

      var mutablearray:nsmutablearray!   let defaults = nsuserdefaults.standarduserdefaults()     override func viewdidload() {      super.viewdidload()        mutablearray = nsmutablearray()       if let haveurl = defaults.objectforkey("u"){          u = haveurl.mutablecopy() as! [nsstring]       }       if let haveimage = defaults.objectforkey("i"){          = haveimage.mutablecopy() as! [nsstring]       }        if let checkavailability = defaults.objectforkey("fav_unfav"){         mutablearray = checkavailability.mutablecopy() as! nsmutablearray      }else{      var check in image {         print(check)         mutablearray.addobject("wheart")      }   } } 

    cellforitematindexpath:

    //add new line cell.heartbt.setimage(uiimage(named: mutablearray[indexpath.row] as! string), forstate: uicontrolstate.normal) 
  2. livecollectionviewcell.swift:

     class livecollectionviewcell: uicollectionviewcell{   var showplaybutton = true   var number = int()  var secondnumber = int()   @iboutlet weak var playbt: uibutton!   @iboutlet weak var imageview: uiimageview!   @iboutlet weak var heartbt: uibutton!   @ibaction func heartbt(sender: uibutton) {   var btn : nsinteger btn = sender.tag nsinteger  let uvalues = url[btn] let ivalues = image[btn]  if mutablearray[btn] as! string == "rheart" {     showplaybutton = false }  if showplaybutton == true{     //add line     mutablearray.replaceobjectatindex(btn, withobject: "rheart")      u.append(url[btn])     i.append(image[btn])     self.heartbt.setimage(uiimage(named: "rheart"), forstate: uicontrolstate.normal)     showplaybutton = false     print("rheart xxxxxxxxxxxxxxx")  }else{     //add line     mutablearray.replaceobjectatindex(btn, withobject: "wheart")      check in u {          number++         print(number)          if uvalues == check{             print(u)             u.removeatindex(number-1)             number = 0             print("remove @ indext path")             print(u)              break          }      }      check2 in i{          secondnumber++         print(secondnumber)          if ivalues == check2{             print(i)             i.removeatindex(secondnumber-1)             secondnumber = 0             print("remove @ indext path")             print(i)             self.heartbt.setimage(uiimage(named: "wheart"), forstate: uicontrolstate.normal)             showplaybutton = true             print("wheart ........")             break          }      }  }    //set array userdefault     defaults.setobject(mutablearray, forkey: "fav_unfav")     defaults.setobject(u, forkey: "u")     defaults.setobject(i, forkey: "i")      defaults.synchronize()    } } 
  3. favoritetvviewcontroller.swift:

    override func viewwillappear(animated: bool) {    collectionview.reloaddata()     if let checkurl = defaults.objectforkey("u") {      uv = checkurl.mutablecopy() as! nsarray    }     if let checkimage = defaults.objectforkey("i"){      iv = checkimage.mutablecopy() as! nsarray    }     print("uv values.. \(uv)")    print("iv values.. \(iv)") } 

Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -