Getting Crash in cellForRowAtIndexPath in tableView on Crashlytics
<p>I got weird crash on Crashlytics, It's showing crash in the line as commented in code.</p>

<pre><code>func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {

    if indexPath.row &gt;= collectionArrayApiObject.collectionsArray.count {
        return UITableViewCell(frame: CGRectZero)
    }

    var tableViewCell:CollectionsTableViewCell? = tableView.dequeueReusableCellWithIdentifier("collectionsCell") as? CollectionsTableViewCell
    if (tableViewCell == nil) {
        tableViewCell = CollectionsTableViewCell(style: .Default, reuseIdentifier: "collectionsCell")
    }
    // Got crash in below line
    let collectionObject = collectionArrayApiObject.collectionsArray[indexPath.row]
    tableViewCell!.setCollection(collectionObject)

    return tableViewCell!
}
</code></pre>

<p>This is the stack trace that I got.. </p>

<p><a href="https://i.stack.imgur.com/T9DvV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/T9DvV.png" alt="Stack Traces"></a></p>

<p>But not getting why this even happened? I have already put check that</p>

<pre><code>index.row &gt;= collectionArrayApiObject.collectionsArray.count
</code></pre>

<p>so it should not be index out of range case. Can any one give me just one case in which this can happen?</p>

<p><strong>NOTE:</strong> This happened with only one user till now but still why this even happened?</p>

<p><strong>Whole Table View Controller:</strong></p>

<pre><code>import UIKit

class CollectionsViewController: CUIBaseViewController, CollectionArrayApiObjectDelegate, UITableViewDataSource, UITableViewDelegate, UIViewControllerPreviewingDelegate {
var collectionsTableView : UITableView?
var collectionArrayApiObject : CollectionArrayApiObject = CollectionArrayApiObject()
var headerSearchButton : UIBarButtonItem?
var cityId: String?

required init?(coder aDecoder: NSCoder) {
    fatalError("NSCoding not supported")
}

override init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!) {
    super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}

convenience init(CityId: String? = nil) {
    self.init(nibName: nil, bundle: nil)
    self.cityId = CityId
    self.view.backgroundColor = Colors.white()
}

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    self.setUpHeaderView()
    self.createTableView()
    self.getData()
    self.listenToNotifications()
    self.automaticallyAdjustsScrollViewInsets = false
    if #available(iOS 9.0, *) {
        registerForPreviewingWithDelegate(self, sourceView: collectionsTableView!)
    }
}

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    self.navigationController?.setNavigationBarHidden(false, animated: animated)
}

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
}

func listenToNotifications() {
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(CollectionsViewController.cityChanged), name: Constants.NOTIFIICATION_LOCATION_CHANGED_CITY, object: nil)
}

func cityChanged() {
    self.getData()
}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    collectionsTableView?.frame = CGRectMake(0, Constants.NAV_BAR_HEIGHT, self.view.width, self.view.height - Constants.NAV_BAR_HEIGHT)
}

//MARK: view creation functions
func setUpHeaderView() {
    self.navigationItem.title = "Collections"

    let negativeSpacer = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonSystemItem.FixedSpace, target: nil, action: nil)
    negativeSpacer.width = 0;

    headerSearchButton = UIBarButtonItem.init(title: "n", style: .Plain, target: self, action: #selector(CollectionsViewController.headerSearchButtonTapped))
    headerSearchButton?.width = 40
    headerSearchButton?.setTitleTextAttributes([NSFontAttributeName: Fonts.iconFont(18), NSForegroundColorAttributeName: Colors.gray()], forState: .Normal)

    self.navigationItem.rightBarButtonItems = [negativeSpacer, headerSearchButton!];
}

func createTableView() {
    collectionsTableView = UITableView(frame: CGRectZero, style: .Plain)
    collectionsTableView?.separatorStyle = .None
    collectionsTableView?.backgroundColor = Colors.white()
    collectionsTableView?.dataSource = self
    collectionsTableView?.delegate = self
    self.view.addSubview(collectionsTableView!)
}

//MARK : tableview delegate methods
func numberOfSectionsInTableView(tableView: UITableView) -&gt; Int {
    return 1;
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
    return collectionArrayApiObject.collectionsArray.count
}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -&gt; CGFloat {
    return CollectionsTableViewCell.getHeight()
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {

    if indexPath.row &gt;= collectionArrayApiObject.collectionsArray.count {
        return UITableViewCell(frame: CGRectZero)
    }

    var tableViewCell:CollectionsTableViewCell? = tableView.dequeueReusableCellWithIdentifier("collectionsCell") as? CollectionsTableViewCell
    if (tableViewCell == nil) {
        tableViewCell = CollectionsTableViewCell(style: .Default, reuseIdentifier: "collectionsCell")
    }

    let collectionObject = collectionArrayApiObject.collectionsArray[indexPath.row]
    tableViewCell!.setCollection(collectionObject)

    return tableViewCell!
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    Commons.trackEvent(Commons.createGAcategory([Constants.COLLECTIONS_PAGE, "collection"]), action: "click", label: nil, value: 1)

    let exploreFeedForCollection = ExploreFeedViewController.init(collection: collectionArrayApiObject.collectionsArray[indexPath.row])

    //self.presentViewController(CUINavigationController.init(rootViewController: exploreFeedForCollection), animated: true, completion: nil)
    self.navigationController?.pushViewController(exploreFeedForCollection, animated: true)
}

func scrollViewDidScroll(scrollView: UIScrollView) {
    let contentOffset = scrollView.contentOffset.y
    if (collectionsTableView != nil) {
        for cell in collectionsTableView!.visibleCells {
            if (cell.isKindOfClass(CollectionsTableViewCell.self)) {
                let cellOffset = cell.y - contentOffset;

                //image parallax
                let parallaxCut: CGFloat = 0.5
                let percent = (cellOffset + cell.height)/(collectionsTableView!.height + cell.height);
                let extraHeight = cell.height * (CollectionsTableViewCell.parallaxRatio-1.0) * parallaxCut;
                let collectionCell = cell as! CollectionsTableViewCell;                    
                collectionCell.bgImageView.y = -extraHeight*percent;
            }
        }
    }

}

//MARK : get cities data functions
func getData() {
    collectionArrayApiObject = CollectionArrayApiObject()
    collectionArrayApiObject.fetchCollections(Delegate: self, CityId: self.cityId)

    if collectionArrayApiObject.collectionsArray.count == 0 {
        self.showLoader("Hmm, things are getting interesting")
    }
}

func collectionsFetchedSuccessfully() {
    self.hideNothingHereViewAndLoader()
    self.collectionsTableView?.reloadData()
    self.scrollViewDidScroll(self.collectionsTableView!)
}

func collectionsFetchingFailed(errorType: ErrorType) {
    self.showNothingHereView(errorType, icon: nil, showTryAgain: true)
}

override func didTapReloadButton() {
    self.getData()
}

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

//MARK: UIViewControllerPreviewingDelegate
func previewingContext(previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -&gt; UIViewController? {
    guard let indexPath = collectionsTableView?.indexPathForRowAtPoint(location) else{
        return nil
    }

    // Lifting cell up on 3D touch before peeking
    if #available(iOS 9.0, *) {
        let cellRect = collectionsTableView?.rectForRowAtIndexPath(indexPath)
        let sourceRect = previewingContext.sourceView.convertRect(cellRect!, fromView: collectionsTableView)
        previewingContext.sourceRect = sourceRect
    }

    return ExploreFeedViewController.init(collection: collectionArrayApiObject.collectionsArray[(indexPath as NSIndexPath).row])
}

func previewingContext(previewingContext: UIViewControllerPreviewing, commitViewController viewControllerToCommit: UIViewController) {
    self.navigationController?.pushViewController(viewControllerToCommit, animated: true)
}

//MARK : header button functions
func headerSearchButtonTapped() {
    let searchVC = SearchViewController(CityId: cityId)
    self.navigationController?.pushViewController(searchVC, animated: true)
}

deinit {
    NSNotificationCenter.defaultCenter().removeObserver(self)
}
</code></pre>

<p>}</p>