2016-05-06 5 views
1

お気に入りの画像を別のUIViewControllerクラスに保存するには、collectionviewcellにお気に入りボタンを追加します。
ボタンを押すと、その画像が赤い画像に変わります。私はCollectionViewCellクラスでボタンアクションを作成しました。 "データ"配列値または現在選択されているインデックス値を "items"配列UIViewControllerに保存したい場合は、ボタンを押したときにセルインデックスパスに従ってクラスを作成します。データをCollectionViewCell配列に追加する方法

このボタンをもう一度押すと、セルインデックスパスに従ってUIViewControllerの "items"配列からこのインデックスパスの値が削除されます。
主な問題は、ViewController型変数がcollectionviewcellで作成されていないことです。
実際には、お気に入りのセルデータをUIViewControllerクラスのような別のCollectionViewに表示しようとしています。

どうすればいいですか?

enter image description here

*

import UIKit 
class LiveCollectionView: UIViewController, UICollectionViewDataSource, DrapDropCollectionViewDelegate { 
    @IBOutlet var dragDropCollectionView: DragDropCollectionView! 


    var objects : [Dictionary<String, String>] = [ 
     ["url" : "https://s3.amazonaws.com/kargopolov/BlueCafe.mp3", "image": "1.jpg"], 
     ["url" : "https://s3.amazonaws.com/kargopolov/BlueCafe.mp3", "image": "2.jpg"], 
     ["url" : "http://2016.downloadming1.com/bollywood%20mp3/Sanam%20Teri%20Kasam%20(2016)/02%20-%20Kheech%20Meri%20Photo%20-%20DownloadMing.SE.mp3", "image": "3.jpg"], 
     ["url" : "http://2016.downloadming1.com/bollywood%20mp3/Sanam%20Teri%20Kasam%20(2016)/03%20-%20Bewajah%20-%20DownloadMing.SE.mp3", "image": "4.jpg"], 
     ["url" : "http://2016.downloadming1.com/bollywood%20mp3/Sanam%20Teri%20Kasam%20(2016)/04%20-%20Tera%20Chehra%20-%20DownloadMing.SE.mp3", "image": "5.jpg"], 
     ["url" : "http://2016.downloadming1.com/bollywood%20mp3/Sanam%20Teri%20Kasam%20(2016)/02%20-%20Kheech%20Meri%20Photo%20-%20DownloadMing.SE.mp3", "image": "6.jpg"], 
     ["url" : "http://media.downloadming.se/TEMP/Loveshhuda%20(2015)/01%20-%20Mar%20Jaayen%20-%20DownloadMing.SE.mp3", "image": "5.jpg"], 
     ["url" : "http://2016.downloadming1.com/bollywood%20mp3/Sanam%20Teri%20Kasam%20(2016)/01%20-%20Sanam%20Teri%20Kasam%20-%20DownloadMing.SE.mp3", "image": "7.jpg"]] 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     dragDropCollectionView.draggingDelegate = self 
     dragDropCollectionView.enableDragging(true) 
    } 

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return objects.count 
    } 

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier("collectionViewCell", forIndexPath: indexPath) as! LiveCollectionViewCell 

     let object = objects[indexPath.row] 
     cell.imageView.image = UIImage(named: object["image"]!) 
     cell.data = object 

     return cell 
    } 

    func dragDropCollectionViewDidMoveCellFromInitialIndexPath(initialIndexPath: NSIndexPath, toNewIndexPath newIndexPath: NSIndexPath) { 
     let colorToMove = objects[initialIndexPath.row] 
     objects.removeAtIndex(initialIndexPath.row) 
     objects.insert(colorToMove, atIndex: newIndexPath.row) 
} 

} 

*

import UIKit 
class LiveCollectionViewCell: UICollectionViewCell{ 

    var showPlayButton = true 

    var data : [String : String]! 


    @IBOutlet weak var imageView: UIImageView! 

    @IBOutlet weak var heartBt: UIButton! 

    @IBAction func heartBt(sender: UIButton) { 


     if showPlayButton { 

      self.heartBt.setImage(UIImage(named: "rHeart"), forState: UIControlState.Normal) 
      showPlayButton = false 
      print(data) 

     } 
     else 
     { 
      self.heartBt.setImage(UIImage(named: "wHeart"), forState: UIControlState.Normal) 
      showPlayButton = true 

     } 


    } 

} 

*

import UIKit 

    class ViewController: UIViewController , UICollectionViewDelegate{ 

     var items : [Dictionary<String, String>]? 

     let reuseIdentifier = "collectionViewCell" 

     override func viewDidLoad(){ 

     } 

     func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int 
     { 
      return self.items!.count 
     } 

     func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> CollectionViewCell 
     { 
      let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CollectionViewCell 

      var object = items![indexPath.row] 


      cell.imageView.image = UIImage(named: object["image"]!) 

      return cell 
     } 


     } 
* 

*

答えて

0

URLと好きな場合はその状態を保持するクラスを作成しようとします。 これらの種類のオブジェクトの配列をCore Date に保存してから、各viewControllerを開くときに、コア日付項目を表示可能な配列にロードします。コアデータ項目を変更してコアデータデータベースに保存します。

関連する問題