user3746428
user3746428

Reputation: 11175

'unexpectedly found nil' for search in UITableView

I recently tried changing my UITableViewController to a UITableView within a UIView. I changed back to this as I was experiencing an error with my UISearchBar, as when I would tap a key to search my app would crash with the error:

fatal error: unexpectedly found nil while unwrapping an Optional value

on this line:

var cell = tableView.dequeueReusableCellWithIdentifier("rideCell")  as! RideCell

When I switched back to the UITableViewController this error went away and everything was fine, however I've just tested it again and it is again giving me that error.

Anyone have any suggestions? It works fine for the normal table view, it's just when I go to do a search that it crashes. The identifier is definitely correct.

Thanks!

EDIT:

Full function:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier("rideCell", forIndexPath: indexPath) as! RideCell

    var ride: Ride

    if tableView == self.searchDisplayController?.searchResultsTableView {
        ride = DataManager.sharedInstance.getRideByName(searchResults[indexPath.row].name)!
    } else {
        ride = DataManager.sharedInstance.rideAtLocation(indexPath.row)!
    }

    cell.rideNameLabel.text = ride.name

    var dateSinceUpdate = NSDate().timeIntervalSinceDate(ride.updated!)
    var secondsSinceUpdate = Int(dateSinceUpdate)
    var timeSinceUpdate = printSecondsConvert(secondsSinceUpdate)

    cell.updatedLabel.text = timeSinceUpdate

    if ride.waitTime == "Closed" {
        cell.waitTimeLabel.text = ride.waitTime!
        cell.timeBackgroundView.backgroundColor = getColorFromNumber(80)
        cell.waitTimeLabel.font = UIFont(name: "Avenir", size: 13)
    } else {
        cell.waitTimeLabel.text = "\(ride.waitTime!)m"
        cell.timeBackgroundView.backgroundColor = getColorFromNumber(ride.waitTime!.toInt()!)
        cell.waitTimeLabel.font = UIFont(name: "Avenir", size: 17)
    }

    AsyncImageLoader.sharedLoader().cancelLoadingURL(cell.rideImageView.imageURL)
    cell.rideImageView.image = UIImage(named: "Unloaded")
    cell.rideImageView.imageURL = NSURL(string: ride.rideImageSmall!)

    return cell
}

Upvotes: 7

Views: 1685

Answers (7)

oOEric
oOEric

Reputation: 1079

Make sure you fill the correct parameters in the code below.

private let cellReuseIdentifier = "MyCell"

tableView.registerNib(UINib(nibName: "MyCell", bundle: nil), forCellReuseIdentifier: cellReuseIdentifier)

Upvotes: 0

Brian Nezhad
Brian Nezhad

Reputation: 6258

There are a few possibilities that you are seeing a fatal error of nil message in your dialog.

Possibility #1: Make sure you have a subclass of UITableViewCell named RideTableViewCell.swift. To create a subclass of UITableViewCell simply follow the procedures below.

  1. Right-Click on your Project name and create New File... in Project Navigator
  2. From iOS->Source create Cocoa Touch Class
  3. In Option Dialog Subclass Field Type UITableViewCell enter image description here
  4. I believe you have an Custom XIB file already, if not, Check Also create XIB file
  5. Make sure you input your XIB identifier in Attribute Inspector

enter image description here

  1. Register your cell class in viewDidLoad() function like so:

    let nibCell = UINib(nibName: "RideTableViewCell", bundle: nil) self.tableView.registerNib(nibPosts, forCellReuseIdentifier: "RideCell")

  2. Register your custom cell in cellForRowAtIndexPath like so:

    let cell: RideTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("RideCell", forIndexPath: indexPath) as! RideTableViewCell


Possibility #2: Maybe when you create your custom XIB you didn't tell it which class it belongs to. To set the class of XIB, follow the procedures below.

  1. Click on your .xib file in Project Navigator
  2. Go to identity inspector of your cell and make sure RideTableViewCell is in there.

enter image description here

Please comment if you have any question. Cheers!

Upvotes: 7

user3746428
user3746428

Reputation: 11175

Discovered an extremely simple solution to the issue. Had to change this:

var cell = tableView.dequeueReusableCellWithIdentifier("rideCell", forIndexPath: indexPath) as! RideCell

to this:

var cell = self.tableView.dequeueReusableCellWithIdentifier("rideCell", forIndexPath: indexPath) as! RideCell

Upvotes: 13

Sri Ram
Sri Ram

Reputation: 35

It is showing nil because there is no UItableViewCell which is of type RideCell. You have to create a new RideCell.swift which will be a subclass of UITableViewCell and then associate that with the cell of your tableView and then proceed .

Upvotes: 1

JustAnotherCoder
JustAnotherCoder

Reputation: 2575

Please check that your delegates have been properly set in viewDidLoad and that you are inheriting delegate methods of UITableView and search functions like so:

class YourClass: UIViewController, UITableViewDelegate, UITableViewDataSource {

    func viewDidLoad() {
       tableView.delegate = self
       tableView.dataSource = self
    }

}

and do the same for the search bar delegates and data sources. More on that here.

Upvotes: 1

Karlos
Karlos

Reputation: 1661

If you are not using UITableViewController, then check the following extension are added or not.

class XYZViewController: UIViewController, UITableViewDataSource, UITableViewDelegate,UISearchBarDelegate, UISearchDisplayDelegate, UISearchResultsUpdating
{

or Check this Tutorial. It might help you to solve this error.

Upvotes: 1

chakshu
chakshu

Reputation: 1392

Please check did you have given proper class name (RideCell), filled proper module(Your target) and finally the identifier in the storyboard. If this is ok please share SS of your storyboard tableView cell.

enter image description here

and enter image description here

Hope it helps

Upvotes: 3

Related Questions