将tableCell内的collectionCell数据传递给secondVC使用协议

时间:2017-09-11 09:29:59

标签: ios swift uitableview collections protocols

我有两个viewController' s。第一个VC在collectionCell内有tableCell。第二个VC有collectionCell,我有快速File,而Model有一个类。我需要将数据从第一个VC(collectionCell)传递到第二个VC。

collectionCell传递数据需要因为collectionCell有图像(来自swift文件 - 模型)。我认为需要使用协议,但我真的不懂如何使用协议。请帮忙。

我的代码:

1stVC (适用的的viewController ):

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()
    }
}
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
    }
}

1stVC tableCell内的collectionCell ):

protocol PostDelegate {
    func selectedPost(cell: String)
}

class TableViewCell1: UITableViewCell {

    var images = [String]()
    var selecetdItem: Model?
    var postDelegate: PostDelegate?

}
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
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        postDelegate?.selectedPost(cell: self.images[indexPath.item])
        print(images[indexPath.item])
    }
}

2ndVC (适用的的viewController ):

class ViewController3: UIViewController {

    @IBOutlet weak var collectionView: UICollectionView!

//    var images = [String]()
    var images: [Model] = []

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}
extension ViewController3: 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! CollectionViewCell3
//        let array = [images[indexPath.item].images,
//                     images[indexPath.item].images2]
//        cell.imagesView.sd_setImage(with: URL(string: array[indexPath.item]))
//        cell.imagesView.sd_setImage(with: URL(string: images[indexPath.item]))
        return cell
    }
}

模型 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)
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

将ViewController1作为TableViewCell1

的postDelegate传递
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell1
      //make VC1 as post delegate of cell
      (cell as! TableViewCell1).postDelegate = self
      cell.images = [image[indexPath.row].image,
                           image[indexPath.row].image2]
      return cell
}

最后实施

extension ViewController1 : PostDelegate {
    func selectedPost(cell: String) {
         //call self.perform segue or load VC2 and pass data here
    }
}

一撮建议:

var postDelegate: PostDelegate?

将导致强烈持有对传递的委托的引用。这将导致内存泄漏。为了使其更安全,将委托声明为weak

weak var postDelegate: PostDelegate?

为了使协议变弱你的协议

protocol PostDelegate: NSObjectProtocol {
    func selectedPost(cell: String)
}