如何从collectionCell中的类模型正确加载数组(image、image2、image3)图像?
类本身的模型如下所示:

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不显示数组(如果我错了,请纠正我),但是如何修复它却不知道。
集合单元格的代码:
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)
        }
    }
}

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

最佳答案

如果下一个ViewController是此单元格的子级,则可以通过这种方式将数组传递给它。
您可以在下一个ViewController的viewDidLoad()中编写此命令:

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

关于ios - 在collectionCell内的collectionCell中使用Class/Struct作为将来的传递数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46278924/

10-11 04:00