0

i need to understand why my code not return the property correctly. i work on UICollectionView i have a custom cell class

    class FileCollectionViewCell: UICollectionViewCell {


    @IBOutlet weak var cellTitle: UILabel!
    @IBOutlet weak var myImage: UIImageView!

    var cellType = "";


}

now in my class UICollectionViewController

    class FileCollectionViewController: UICollectionViewController, UITextFieldDelegate,
  UICollectionViewDelegateFlowLayout{


    let reuseIdentifier = "Cell"


    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        //#warning Incomplete method implementation -- Return the number of sections
        return 1;
    }


    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        //#warning Incomplete method implementation -- Return the number of items in the section

        return 5
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as FileCollectionViewCell

        // Configure the cell
         cell.backgroundColor = UIColor.blackColor()

         cell.cellType = "PDF";// I set my cellType here
         println(cell.cellType);// here it's ok i get my value

         cell.myImage?.image = UIImage(named: "toto");
         cell.cellTitle.text = "text";

        return cell
    }



    override  func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){
            println("index : \(indexPath.row)");
            let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as FileCollectionViewCell



            println(cell.cellType) // here i would like retrieve "PDF" but it's empty...

        }

}

When i touch the cell the cellType is not return , empty is return ???

thank a lot

user2718075
  • 333
  • 2
  • 4
  • 15

3 Answers3

0

It seems you are dequeuing another cell rather than retrieving the one that already exists. Use UICollectionView's cellForRowAtIndex function to retrieve the specific cell

let cell = collectionView.cellForRowAtIndexPath(indexPath) as! FileCollectionViewCell
println(cell.cellType)
Ian
  • 12,538
  • 5
  • 43
  • 62
0

thank

you are right.

i replace

let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as FileCollectionViewCell

for

let cell = collectionView.cellForItemAtIndexPath(indexPath) as FileCollectionViewCell

thank

user2718075
  • 333
  • 2
  • 4
  • 15
0

You are dequeuing a cell for reuse. dequeueReusableCellWithReuseIdentifier() has different purpose, normally meant to be used in

override func collectionView(collectionView: UICollectionView, 
        cellForItemAtIndexPath indexPath: NSIndexPath) -> FileCollectionViewCell {
    ...
}

If you want to get an existing cell and its data, you should use cellForItemAtIndexPath()

override func collectionView(collectionView: UICollectionView, 
        didSelectItemAtIndexPath indexPath: NSIndexPath) {
    let cell = collectionView.cellForItemAtIndexPath(indexPath) as FileCollectionViewCell

    // then you should be able to get your value
    println(cell.cellType)
}
Jonauz
  • 4,133
  • 1
  • 28
  • 22