【问题标题】:'unexpectedly found nil' for search in UITableView在 UITableView 中搜索“意外发现 nil”
【发布时间】:2015-07-31 21:43:42
【问题描述】:

我最近尝试将UITableViewController 更改为UITableView 内的UIView。我改回这个,因为我的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
}

【问题讨论】:

  • 您需要针对 RideCell 类注册单元重用标识符 rideCell - 如果您使用情节提要,则通过原型单元执行此操作
  • 然后仔细检查所有内容,因为异常表明 dequeueReusableCellWithIdentifier 返回 nil - 这意味着单元重用标识符未注册到类。确保场景中的表格视图链接到视图控制器中的 IBOutlet。
  • 我无法再告诉您您所显示的内容 - 如果该行返回 nil,那是因为该函数调用中的 tableview 没有为该重用注册的类标识符。
  • 您可能只需要进行更多调试 - 检查 tableView 的值是否与您的 IBOutlet 相同 - 它可能是不同的表视图
  • if tableview == ... 行让我相信您有多个 tableview,因此您可能将单元格排在错误的 tableview 上,或者您需要在另一个 tableview 上注册重用标识符好

标签: ios swift uitableview uisearchbar


【解决方案1】:

发现了一个非常简单的解决方案。不得不改变这个:

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

到这里:

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

【讨论】:

  • 不错的答案。从字面上不知道为什么会这样,但它确实有效!
  • 我会为此拼命撞墙。不知道为什么会这样。他们不完全一样
  • 这是因为它试图使用tableView作为参数传递,这是搜索框自己的表格视图。 self.tableView 强制它使用主表视图。
【解决方案2】:

您可能会在对话中看到 fatal errornil 消息。

可能性#1:确保您有一个名为RideTableViewCell.swiftUITableViewCell 子类。 要创建 UITableViewCell 的子类,只需按照以下步骤操作即可。

  1. 右键单击您的项目名称并在其中创建 New File... 项目导航器
  2. iOS->Source 创建 Cocoa Touch 类
  3. 在选项对话框子类字段类型UITableViewCell
  4. 我相信您已经有一个自定义 XIB 文件,如果没有,请检查 同时创建 XIB 文件
  5. 确保在 Attribute Inspector 中输入您的 XIB identifier

  1. 像这样在viewDidLoad()函数中注册你的单元格类:

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

  2. cellForRowAtIndexPath 中注册您的自定义单元格,如下所示:

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


可能性#2:也许当您创建自定义 XIB 时,您没有告诉它它属于哪个类。设置 XIB 的类,请按以下步骤操作。

  1. Project Navigator 中单击您的 .xib 文件
  2. 去你的牢房的身份检查员,确保RideTableViewCell在那里。

如果您有任何问题,请发表评论。干杯!

【讨论】:

    【解决方案3】:

    请检查您是否给出了正确的类名(RideCell),填写了正确的模块(您的目标),最后是故事板中的标识符。如果可以,请分享您的故事板 tableView 单元格的 SS。

    希望对你有帮助

    【讨论】:

      【解决方案4】:

      如果您没有使用 UITableViewController,请检查是否添加了以下扩展。

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

      或检查此Tutorial。它可能会帮助您解决此错误。

      【讨论】:

        【解决方案5】:

        请检查您的委托是否已在 viewDidLoad 中正确设置,并且您正在继承 UITableView 的委托方法和搜索功能,如下所示:

        class YourClass: UIViewController, UITableViewDelegate, UITableViewDataSource {
        
            func viewDidLoad() {
               tableView.delegate = self
               tableView.dataSource = self
            }
        
        }
        

        并对搜索栏委托和数据源执行相同的操作。更多关于 here.

        【讨论】:

          【解决方案6】:

          显示 nil 是因为没有 RideCell 类型的 UItableViewCell。您必须创建一个新的 RideCell.swift,它将是 UITableViewCell 的子类,然后将其与 tableView 的单元格相关联,然后继续。

          【讨论】:

            【解决方案7】:

            确保在下面的代码中填写正确的参数。

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2014-09-10
              • 2020-11-27
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2017-04-10
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多