ios - 使用 Class/Struct in collection Cell inside collection Cell for future pass data

标签 ios swift class collections

如何从 collectionCell 中的类 Model 正确加载数组(image、image2、image3)图像?

类本身 Model 如下所示:

class Model {

    var image: String
    var image2: String
    var image3: String
    var images: [String] = []
    var images2: [String] = []
    var images3: [String] = []
    var ref: FIRDatabaseReference!

    init(snapshot: FIRDataSnapshot) {
        ref = snapshot.ref
        let value = snapshot.value as! NSDictionary
        let snap = value["hall1"] as? NSDictionary
        let snap2 = value["hall2"] as? NSDictionary
        let snap3 = value["hall3"] as? NSDictionary
        image = snap?["qwerty"] as? String ?? ""
        image2 = snap2?["qwerty"] as? String ?? ""
        image3 = snap3?["qwerty"] as? String ?? ""
        if let post1 = snap as? [String: AnyObject] {
            for (_, value) in post1["images"] as! [String: AnyObject] {
                self.images.append(value as! String)
            }
        }
        if let post2 = snap2 as? [String: AnyObject] {
            for (_, value) in post2["images"] as! [String: AnyObject] {
                self.images2.append(value as! String)
            }
        }
        if let post3 = snap3 as? [String: AnyObject] {
            for (_, value) in post3["images"] as! [String: AnyObject] {
                self.images3.append(value as! String)
            }
        }
    }
}

在我的 collectionCell 中只加载了第一张图片,我基本上理解为什么,据我了解这是因为 sd_setImage 不显示数组(如果我错了),但不知道如何解决。

collectionCell代码:

class CollectionViewCell11: UICollectionViewCell, UICollectionViewDelegate, UICollectionViewDataSource {

    var imagess: [Model] = []
    @IBOutlet weak var collectionView: UICollectionView!

    var vc1: ViewController?

    override func awakeFromNib() {
        super.awakeFromNib()

        collectionView.delegate = self
        collectionView.dataSource = self
    }

    func collectionView(_ collectionView: UICollectionView, numb  erOfItemsInSection section: Int) -> Int {
        return imagess.count
}
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell12
        cell.imageView.sd_setImage(with: URL(string: imagess[indexPath.item].image))

        return cell
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if vc1 != nil {

            let vc2 = vc1!.storyboard?.instantiateViewController(withIdentifier: "ViewController2") as! ViewController2

            vc2.photo = [imagess[indexPath.item]]

            let backItem = UIBarButtonItem()
            backItem.title = ""
            vc1!.navigationItem.backBarButtonItem = backItem

            vc1!.navigationController?.pushViewController(vc2, animated: true)
        }
    }
}

要使用我只需要 Model 类,因为它包含另一个图像数组,我计划显示下一个滚动图像 Controller 。

最佳答案

如果您的下一个 ViewController 是此单元格的子级,那么您可以通过这种方式将数组传递给它。

你在下一个 ViewController 的 viewDidLoad() 中写这个:

var newImagesArray: [Model] = []
if let parentVC = self.parent as? CollectionViewCell11 {
    newImagesArray = parentVC.imagess 
}

关于ios - 使用 Class/Struct in collection Cell inside collection Cell for future pass data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46278924/

相关文章:

ios - 快速播放录制的音频

ios - Ad Hoc 应用程序标识符权利不匹配?

ios - 如何在 swift 中初始化一个空的 NSMutableAttributedString?

ios - dequeueReusableCell 导致文本改变颜色

javascript - 从对象生成类 (JavaScript)

ios - 将图像添加到我的 UIButton subview

ios - 可选类中的可选属性 VS 可选字典中的可选值

ios - 根据 URL 前缀执行不同的操作

css - 在 WooCommerce/WordPress 中使用 CSS 定位产品属性

java - 处理/Java 中的 "Enclosing type"错误