在sd_setImage中传递一个数组

时间:2017-09-07 19:07:19

标签: ios swift segue uicollectionviewcell

如何使用 sg_setImage (collectionCell)传递到第二个控制器(collectionCell) > class (swift文件)使用 segue ? 要将图像数组加载到第一个控制器上,我在 tableCell 中使用 collectionCell

ViewController (firstVC):

class ViewController1: UIViewController {

    @IBOutlet weak var tableView: UITableView!

    var image: [Model] = []
    var ref: FIRDatabaseReference!

    override func viewDidLoad() {
        super.viewDidLoad()

        ref = FIRDatabase.database().reference(withPath: "Студии2")

        ref.observe(.value, with: { (snapshot) in
            var newImages: [Model] = []

            for item in snapshot.children {
                let object = Model(snapshot: item as! FIRDataSnapshot)
                newImages.append(object)
            }
            self.image = newImages
            self.tableView.reloadData()
        })
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "segue1" {
            if let indexPaths = self.tableView.indexPathForSelectedRow {
                let indexPath = indexPaths as NSIndexPath
                let dvc = segue.destination as! ViewController2
                dvc.photo = [image[indexPath.row]]
            }
        }
    }
}
extension ViewController1: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return image.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell1
        cell.images = [image[indexPath.row].image,
                       image[indexPath.row].image2]
        return cell
    }
}

tableViewCell (firstVC):

class TableViewCell1: UITableViewCell {

    var images = [String]()

    @IBOutlet weak var collectionView: UICollectionView!
}
extension TableViewCell1: UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return images.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell1

        cell.imagesView.sd_setImage(with: URL(string: images[indexPath.item]))

        return cell
    }
}

collectionViewCell (secondVC):

class ViewController2: UIViewController {

    @IBOutlet weak var collectionView: UICollectionView!

    var photo: [Model] = []

    var ref: FIRDatabaseReference!

    override func viewDidLoad() {
        super.viewDidLoad()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}
extension ViewController2: UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return photo.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell2
        let array = [photo[indexPath.item].image,
                     photo[indexPath.item].image2]
        cell.imagesView.sd_setImage(with: URL(string: array[indexPath.item]!))
//        cell.imagesView.sd_setImage(with: URL(string:            photo[indexPath.item].image2))
        return cell
    }
}

class Model (swift文件):

class Model {
    var image: String!
    var image2: String!
    var images: [String] = []
    var images2: [String] = []
    var ref: FIRDatabaseReference!

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

0 个答案:

没有答案