我最近尝试将UITableViewController更改为UIView中的UITableView.当我遇到UISearchBar的错误时,我改回了这一点,因为当我点击一个键来搜索我的应用程序时会因错误而崩溃:
fatal error: unexpectedly found nil while unwrapping an Optional value
在这条线上:
var cell = tableView.dequeueReusableCellWithIdentifier("rideCell") as! RideCell
当我切换回UITableViewController时,这个错误消失了,一切都很好,但是我刚刚再次测试了它,它再次给了我那个错误.
有人有什么建议吗?它适用于普通的表视图,就在我进行搜索时它会崩溃.标识符绝对正确.
谢谢!
编辑:
全功能:
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 }
解决方法
发现了一个非常简单的问题解决方案.不得不改变这个:
var cell = tableView.dequeueReusableCellWithIdentifier("rideCell",forIndexPath: indexPath) as! RideCell
对此:
var cell = self.tableView.dequeueReusableCellWithIdentifier("rideCell",forIndexPath: indexPath) as! RideCell