Versuche, einen Favoriten-Button zu erstellen, um Elemente in die Favoritenliste aufzunehmen

Ich versuche, eine Favoritenschaltfläche in CollectionViewCell zu erstellen, um Elemente in der Favoritenliste zu speichern. Wenn ich auf die Favoritensymbolschaltfläche tippte, wurde das ausgewählte Zellenelement erfolgreich zum Array der Favoritenlisten-Klasse hinzugefügt und in der Favoritenliste angezeigt. Wenn ich auf die Favoritentaste tippe, ändert sich das Bild, je nachdem, ob die Werte bereits gespeichert sind oder nicht. Wenn ich versuche, Favoritenlistenarray in NSUserDefaults zu speichern, speichert es und zeigt auch Werte an, wenn ich es abrufe. Aber wenn ich meine App vom Simulator aus schließe und erneut starte, werden keine Werte im Favoritenlisten-Array angezeigt, die ich in NSUserDefaults gespeichert habe, und ich möchte auch den Status des Schaltflächenbilds gemäß dem bereits hinzugefügten oder nicht hinzugefügten Element festlegen.

Ausgab Als ich auf Favorit getippt habe. Und nach dem Schließen meiner App erneut ausführen. Kann mir jemand sagen, wie ich das beheben kann. Oder kann jemand das bitte in meinem @ behebProjekt Vielen Dan

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] as 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 as 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{

        for check in u {

            number++
            print(number)

            if uValues == check{
                print(u)
                u.removeAtIndex(number-1)
                number = 0
                print("RemoVe at indext path")
                print(u)
                break

            }

        }

        for check2 in i{

            secondNumber++
            print(secondNumber)

            if iValues == check2{
                print(i)
                i.removeAtIndex(secondNumber-1)
                secondNumber = 0
                print("RemoVe at indext path")
                print(i)
                self.heartBt.setImage(UIImage(named: "wHeart"), forState: UIControlState.Normal)
                showPlayButton = true
                print("wHeart ........")
                break

            }

        }

    }

}
}
import UIKit
var u : [NSString] = []
var i : [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!
}

Antworten auf die Frage(2)

Ihre Antwort auf die Frage