代码之家  ›  专栏  ›  技术社区  ›  Tom

将变量直接从另一个视图控制器传递到UICollectionViewCell

  •  0
  • Tom  · 技术社区  · 8 年前

    每当我转到UICollectionViewController时,我都试图将图像从视图控制器传递到UICollectionViewCell。通常,我只需要使用以下代码将变量直接传递给UICollectionViewController。

    let myCollectionViewController = MyCollectionViewController(collectionViewLayout: UICollectionViewFlowLayout())       
        myCollectionViewController.selectedImage = myImageView?.image         
        navigationController?.pushViewController(myCollectionViewController, animated: true)
    

    import UIKit
    
    
    class myCollectionViewCell: UICollectionViewCell {
    
    let imageView:UIImageView = {
    
        let iv = UIImageView()
        iv.backgroundColor = .red
        iv.contentMode = .scaleAspectFill
        iv.clipsToBounds = true
        return iv
    }()
    
    var selectedImage: UIImage? {
    
        didSet {
    
            self.imageView.image = selectedImage
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    
    
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
     }
    }
    

    1 回复  |  直到 8 年前
        1
  •  -1
  •   Tushar Sharma    8 年前

    希望这能帮助你-:

     import Foundation
    class HomeController: UIViewController{
    
    // STORED VARIABLE FOR CollectionView
    
        lazy var CollectionView : UICollectionView = {
            var layout = UICollectionViewFlowLayout()
            layout.minimumLineSpacing = 0
            var collectionViews = UICollectionView(frame: .zero, collectionViewLayout: layout)
            collectionViews.translatesAutoresizingMaskIntoConstraints = false
            collectionViews.backgroundColor = UIColor.white
            collectionViews.showsHorizontalScrollIndicator = false
            collectionViews.showsVerticalScrollIndicator = false
            collectionViews.dataSource = self
            collectionViews.delegate = self
    
            return collectionViews
        }()
    
        // APPLY CONSTRAINTS FOR CollectionView
    
        func setUpCollectionView(){
    
            view.addSubview(CollectionView)
            CollectionView.register(HomeControllerCell.self, forCellWithReuseIdentifier: "cell")
            CollectionView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
            CollectionView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
            CollectionView.topAnchor.constraint(equalTo: view.topAnchor,constant:92).isActive = true
            CollectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor,constant:-50).isActive = true
    }
    
        override func viewDidLoad() {
            super.viewDidLoad()
            setUpCollectionView()
        }
    
    
    }
    
    // EXTENSION FOR COLLECTION VIEW PARENT
    extension HomeController:UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout{
    
    
        // NUMBER OF SECTION IN TABLE
        public func numberOfSections(in collectionView: UICollectionView) -> Int{
            return 1
        }
        // NUMBER OF ROWS IN PARENT SECTION
        public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
            return 5
    
        }
        // The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
    
        public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
            let Cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! HomeControllerCell
        // PASS IMAGE (whatever you have) TO COMPUTED VARIABLE image
            Cell.image = pass image here
            return Cell
        }
    
        // SIZE FOR PARENT CELL COLLECTION VIEW
        public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize{
            return CGSize(width: view.frame.width, height: 220)
        }
    
    }
    

    CollectionViewCellClass-:

     class HomeControllerCell: UICollectionViewCell {
    
        //INITIALIZER
        override init(frame: CGRect) {
            super.init(frame: frame)
            setUpview()
    
        }
    
    // STORED VARIABLE imageVIEW
    
       let imageView:UIImageView = {
    
        let iv = UIImageView()
        iv.backgroundColor = .red
        iv.contentMode = .scaleAspectFill
        iv.translatesAutoresizingMaskIntoConstraints = false
        iv.clipsToBounds = true
        return iv
    }()
    
    // COMPUTED VARIABLE image
    
    var image: UIImage? {
    
        didSet {
    
            self.imageView.image = image
        }
    }
        // APPLY CONSTRAINTS FOR CELL VIEWS
    
        func setUpview(){
    
        // ADD imageView AS SUBVIEW
    
            addSubview(imageView)
    
        // APPLY CONSTRAINT ON IMAGE VIEW
    
            imageView.leftAnchor.constraint(equalTo: self.leftAnchor,constant:5).isActive = true
            //menuHeader.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
            imageView.topAnchor.constraint(equalTo: self.topAnchor,constant:12).isActive = true
            imageView.heightAnchor.constraint(equalToConstant: 50).isActive = true
            imageView.widthAnchor.constraint(equalToConstant: 50).isActive  = true
        }
    
        // REQUIRED INIT
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    
    }
    
    推荐文章