Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'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
}
like image 813
user3746428 Avatar asked Jul 31 '15 21:07

user3746428


1 Answers

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
like image 181
user3746428 Avatar answered Sep 30 '22 11:09

user3746428