2017-08-06 6 views
0

UICollectionViewControllerにセグメンテーションするたびに、ビューコントローラからUICollectionViewCellに画像を渡そうとしています。通常は、変数を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") 
} 
} 

は、どのように私は直接セグエ中に私のUICollectionViewCellサブクラスに直接自分のイメージを渡すのですか?

+2

あなたはセグエのセルに画像を直接渡すことはできません。細胞はまだ存在しません。ビューコントローラに値を渡すことができ、セルを作成するときに使用できます – Paulw11

+0

したがって、サブクラス化されたセル内で初期化するのではなく、myCollectionViewControllerでimageViewを初期化する必要がありますか? – Tom

+0

これはコレクションビューのcellForRowAtメソッドから行うことができます。 –

答えて

-1

希望これはyou-が役立ちます:

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

私は落選しませんでしたが、あなたの答えが*何も説明していないからかもしれません。確かに、それはいくつかのコード(途中で少なくとも1つの間違いがある)ですが、それはその質問に大まかに関連しているようです。 – DonMag

+0

@DonMag上記の私の間違いを知らせてくれれば大変お世話になります。 –

関連する問題