Warning: file_put_contents(/datas/wwwroot/jiajiahui/core/caches/caches_template/2/default/show.php): failed to open stream: Permission denied in /datas/wwwroot/jiajiahui/core/libraries/classes/template_cache.class.php on line 55

Warning: chmod(): Operation not permitted in /datas/wwwroot/jiajiahui/core/libraries/classes/template_cache.class.php on line 56
从另一个视图控制器直接传递变量到UICollectionViewCell - 源码之家

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

问题描述:

我试图从视图控制器传递图像到UICollectionViewCell,只要我继续到UICollectionViewController。通常,我只是使用下面的代码将变量直接传递给UICollectionViewController。从另一个视图控制器直接传递变量到UICollectionViewCell

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

不过,我已经子类我UICollectionViewCell,并建立了细胞如下:

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

如何直接直接SEGUE期间通过我的图片我的UICollectionViewCell子?

+2

您不能直接传递的图像在SEGUE细胞;该单元格尚不存在。你可以将值传递给视图控制器,它可以在创建单元格时使用它 – Paulw11

+0

因此,我必须在myCollectionViewController中初始化imageView,而不是在subclassed单元格内初始化它? – Tom

+0

您可以通过cellForRowAt方法收集视图。 –

希望这有助于你 - :

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

} 
+0

下来选民请给出一个理由。 –

+0

我没有投票,但也许是因为你的答案不解释任何事情。当然,这是一些代码(顺便说一句,至少有一个错误),但它似乎只与这个问题松散相关。 – DonMag

+0

@DonMag让我知道我上面的错误,这将是非常有益的感谢。 –