0

I've subclassed a UICollectionViewCell in which there is a UIImageView. This image view is connected to an image view control on the story board. This is the subclass:

class ProfilesCell: UICollectionViewCell {
    @IBOutlet weak var ProfileImage: UIImageView!

    func setCell(item:Profile) {
        ProfileImage.image = UIImage(named: item.profileThumb)
        ProfileImage.layer.borderWidth = 0.5
        ProfileImage.layer.borderColor = UIColor.grayColor().CGColor
    }
}

I've created an instance of it and it works fine, but what about another instance? how can I choose it as a custom class for another UICollectionViewCell without reconnecting the ProfileImage to another control?

Or shall I connect that IBOutlet to the UIImageView at the first place?

Storyboard: enter image description here

Maysam
  • 7,246
  • 13
  • 68
  • 106
  • can you clarify what your question is? Are you trying to re-use one imageview across multiple colelctionVIews? or do you want different images in each view? – bolnad Feb 02 '16 at 18:36
  • @bolan I want to reuse it across multiple collectionviews – Maysam Feb 02 '16 at 18:40
  • then how your doing it is ok If you have hooked up your prototype cell to the tableview properly, it will use that cell instead of UIColelctionViewCell. and it will reference profileImage as a UIImageView. Make sure on the storyboard you have overwriten the class name as your CustomClass name – bolnad Feb 02 '16 at 18:43
  • might want to post some storyboard pics if you're unsure if you did it correctly – bolnad Feb 02 '16 at 18:44
  • @bolnad as you can see, `UIImageView` in the `MalesCV` is connected to the `ProfilesCell`, I have another collectionview just like `MalesCV` and want to use it with this subclass. – Maysam Feb 02 '16 at 18:50
  • if you want to use the CollectionViewCell across multiple collectionviews, then you should make a separate .xib file and load it into each collectionview that you want to re-use it. – bolnad Feb 03 '16 at 01:19
  • Write it in a new answer then i can mark it as the answer.@bolnad – Maysam Feb 03 '16 at 04:42

2 Answers2

1

This image view [from the cell] is connected to an image view control on the story board.

I assume that you are talking about a UICollectionViewCell prototype. In this case, the object that you manipulate in the story board is not the collection view cell, it's a prototype of all image view cells in your UIImageView.

I've created an instance of it and it works fine, but what about another instance?

This is precisely the scenario the designers of Swift had in mind, so everything should work the way you expect. When you call dequeueReusableCellWithReuseIdentifier: method, Cocoa instantiates the cell for you, "wires up" your ProfileImage to its UIImageView outlet, and gives you a cell that is ready for use.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • When I use this subclass for the second collectionview, I face this error: ```fatal error: unexpectedly found nil while unwrapping an Optional value``` – Maysam Feb 02 '16 at 19:04
  • @Maysam Did you add your cell to Xcode as a prototype cell? [Use this walk-through for an example](http://www.techotopia.com/index.php/An_iOS_7_Storyboard-based_Collection_View_Tutorial#Adding_the_Collection_View_Cell_Class_to_the_Project). – Sergey Kalinichenko Feb 02 '16 at 19:10
  • Yes I did, check the storyboard picture. If I remove the second collectionview everything works. – Maysam Feb 02 '16 at 19:21
  • 1
    @Maysam I didn't see the update about multiple collection views reusing the same class. See [this Q&A](http://stackoverflow.com/q/9245969/335858), answer #2, for information on how this can be done. The Q&A talks about `UITableViewCell` objects, but it is easy to adapt to collection view cells. – Sergey Kalinichenko Feb 02 '16 at 19:30
1

If you want to use a UICollectionViewCell across multiple UICollectionView, you should create the UICollectionViewCell in a separate .xib. In this case you will have ProfileCell.xib, and ProfileCell.swift. In the .xib file you drag an instance of a UICollectionViewCell onto the screen and set it up the way you would as a prototype cell on a CollectionView.

UICollectionViewCell on xib

Then you will need to register the cell with your collectionview.

    //set this up in viewDidLoad
    let nib = UINib(nibName: "ProfileCell", bundle: nil)
    self.collectionView!.registerNib(nib, forCellWithReuseIdentifier: "ProfileCell")

Then in cellForItemAtIndexPath just reference the cell in the normal way

 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    cell =    self.collectionView?.dequeueReusableCellWithReuseIdentifier("ProfileCell, forIndexPath: indexPath) as! ProfileCell
    cell.setCell()
    return cell
 }
bolnad
  • 4,533
  • 3
  • 29
  • 41