【问题标题】:How to append data CollectionViewCell array to UIViewController array如何将数据 CollectionViewCell 数组附加到 UIViewController 数组
【发布时间】:2016-09-01 17:12:38
【问题描述】:

我在collectionviewcell 上添加了收藏按钮,用于将收藏的图像保存在另一个UIViewController 类中。
当我按下按钮时,其图像变为红色图像。我在CollectionViewCell 类中创建了按钮操作,我想在按下按钮时根据单元格索引路径将“数据”数组值或当前选定的索引值存储在UIViewController 类的“项目”数组中。

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

谁能告诉我该怎么做?

*

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
        }


        }
*

*

【问题讨论】:

    标签: ios swift uicollectionview


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-03
      • 2021-08-11
      • 1970-01-01
      • 1970-01-01
      • 2017-07-07
      • 1970-01-01
      相关资源
      最近更新 更多