IBOutlets in UICollectionView are nil - Swift

Astha Gupta

When I print the IBOutlets I declared in MyCell class, they come out to be nil.

Methods in ViewController

public override func viewDidLoad() {
    super.viewDidLoad()
    self.seriesCollectionView.register(MyCell.self, forCellWithReuseIdentifier: "MyCell")
}

public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath) as! MyCell
    cell.setup()

    return cell
}

MyCell class

class MTSeriesViewCell: UICollectionViewCell {
@IBOutlet weak var cellView: UIView!

@IBOutlet weak var cellImageView: UIImageView!
override func awakeFromNib() {
    super.awakeFromNib()
}

public func setup() {
    print(cellView)
}

}

Is there any other way I should be initializing/registering MyCell class in viewDidLoad of ViewController?

kamil3

Instead of:

self.seriesCollectionView.register(MyCell.self, forCellWithReuseIdentifier: "MyCell")

use:

self.seriesCollectionView.register(UINib(nibName: "MyCellXibName", bundle: nil), forCellWithReuseIdentifier: "MyCell")

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Swift: IBoutlets are nil in Custom Cell

Storyboard / Swift all IBOutlets and _view are nil

Swift load UIViewController from xib IBOutlets nil

Populating UICollectionView with images is returning nil : Swift 2

Private IBOutlets Swift

UICollectionView is nil

UiCollectionView must be initialized with a non-nil layout parameter Swift 3

viewForSupplementaryElementOfKind in UICollectionView returns nil and crashes app using swift

Swift UITableView / UICollectionVIew passing data to cell in cellForRowAt returns nil

UITableViewCell nil IBOutlets when UITableView is declared in nib

UICollectionView cellForItemAtIndexPath is nil

UICollectionVIew NIL after instantiation

UICollectionview cellForItem return nil

Swift - Flipping view controllers disables IBOutlets

How to place IBOutlets in a two dimensional array in swift?

Swift - Unit testing functions that involve IBOutlets?

How to access UILabel without IBOutlets. Swift

Update layout constraints programmatically without IBOutlets - Swift

Can I use @IBOutlets in Protocols (Swift)

Why will didDeselectItemAt of UICollectionView throw an error of unexpected found nil and crash the app Swift?

swift - UICollectionView issue "fatal error: unexpectedly found nil while unwrapping an Optional value"

All IBOutlets become nil after switching to Xcode 6 Beta 5

Xib's IBOutlets returning nil when using Xib in storyboard

Custom container: @IBOutlets nil even though view is displaying correctly

TabBarController's second tab doesn't get instantiated -> IBOutlets are nil

UICollectionView reuseIdentifier in sizeForItemAtIndexPath - swift

UICollectionView sticky header in swift

UiCollectionView Spacing issue - swift

UICollectionView into UIView in Swift