如何将数据CollectionViewCell数组附加到UIViewController数组

时间:2016-05-06 14:59:14

标签: ios swift uicollectionview

我在collectionviewcell上添加了收藏夹按钮,用于在另一个UIViewController课程中保存我喜爱的图像 当我按下按钮时,其图像变为红色图像。我已经在CollectionViewCell类中创建了按钮操作,并且我希望在按下按钮时根据单元索引路径将“数据”数组值或当前选定的索引值存储在UIViewController类的“items”数组中。

当我再次按下此按钮时,它会根据单元格索引路径从UIViewController的“items”数组中删除此索引路径的存储值。
主要问题是ViewController类型变量是不是在collectionviewcell上创建的? 实际上我试图在另一个CollectionView中显示喜欢的单元格数据,如UIViewController Class。

有人可以告诉我我该怎么做吗?

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
        }


        }
*

*

1 个答案:

答案 0 :(得分:0)

尝试创建一个类,该类将保存URL和状态(如果喜欢或不喜欢)。 在Core Date中保存这些对象的数组 ,然后当您打开每个viewController时,将核心日期项加载到可以显示的数组中。当您进行更改时,将核心数据项保存到核心数据数据库。