我最近尝试将 UITableViewController
更改为 UIView
中的 UITableView
。当我的 UISearchBar
遇到错误时,我改回了这个,因为当我点击一个键来搜索时,我的应用程序会因错误而崩溃:
fatal error: unexpectedly found nil while unwrapping an Optional value
在这一行:
var cell = tableView.dequeueReusableCellWithIdentifier("rideCell") as! RideCell
当我切换回 UITableViewController
时,这个错误消失了,一切都很好,但是我刚刚再次测试它,它又给我这个错误。
有人有什么建议吗?它适用于普通表格 View ,只是当我进行搜索时它崩溃了。标识符绝对正确。
谢谢!
编辑:
完整功能:
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
关于ios - 在 UITableView 中搜索“意外发现 nil”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31755652/