【问题标题】:Search Bar in Table View Swift表视图 Swift 中的搜索栏
【发布时间】:2014-09-27 01:04:20
【问题描述】:

我在 Xcode 6 中做一个简单的项目,我想在 tableviewcontroller 中添加搜索栏,但有些东西不适合我。我是按照本教程做的 http://www.raywenderlich.com/76519/add-table-view-search-swift

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if tableView == self.searchDisplayController!.searchResultsTableView {
        return self.filteredProgramy.count
    } else {
        return self.programy.count
    }
}

这里我收到错误“致命错误:在展开可选值时意外发现 nil”。我知道为什么。完整代码在这里

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if tableView == self.searchDisplayController!.searchResultsTableView {
        return self.filteredProgramy.count
    } else {
        return self.programy.count
    }
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell

    var program : Program

    if tableView == self.searchDisplayController!.searchResultsTableView {
        program = filteredProgramy[indexPath.row]
    } else {
        program = programy[indexPath.row]
    }


func filterContentForSearchText(searchText: String) {
    // Filter the array using the filter method
    var scope = String()
    self.filteredProgramy = self.programy.filter({( program: Program) -> Bool in
        let categoryMatch = (scope == "All") || (program.category == scope)
        let stringMatch = program.name.rangeOfString(searchText)
        return categoryMatch && (stringMatch != nil)
    })
}

func searchDisplayController(controller: UISearchDisplayController!, shouldReloadTableForSearchString searchString: String!) -> Bool {
    self.filterContentForSearchText(searchString)
    return true
}

func searchDisplayController(controller: UISearchDisplayController!, shouldReloadTableForSearchScope searchOption: Int) -> Bool {
    self.filterContentForSearchText(self.searchDisplayController!.searchBar.text)
    return true
}

}

【问题讨论】:

  • 该错误与搜索栏无关,与了解如何正确使用选项有关。你得到哪一行的错误?您可能正在对可选值进行强制解包并获得 nil(如错误消息所述)。
  • 这里。 " if tableView == self.searchDisplayController!.searchResultsTableView {"
  • 你解决了吗?
  • self.searchDisplayController 为零吗?
  • 好的。现在我修好了。我再次浏览了您的答案,并修复了它。非常感谢男人!我没有看到“self.searchDisplayController 为零。”

标签: ios swift xcode tableview searchbar


【解决方案1】:

self.searchDisplayController 为零。

我刚刚下载了教程的示例代码(你也应该这样做),我看到作者在他们的 nib 文件中有一个“搜索显示控制器”。检查您的 nib 文件并确保 Candy Search 控制器已正确连接。

它应该是这样的:

要访问该图像,请右键单击 .xib 文件中的 Search Display Controller 对象。请注意,在我的图像中,“参考插座”在 searchDisplayController 和 CandySearch 之间有联系。这就是你所缺少的。

要创建连接 ctrl 从CandySearch 控制器拖动到“搜索显示控制器”,当您松开鼠标时,您会看到:

点击searchDisplayController,您应该一切顺利。

最后,你应该阅读一下可选函数在 Swift 中的工作方式,以帮助你理解未来这样的问题:

https://developer.apple.com/librarY/prerelease/mac/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-XID_456

【讨论】:

    【解决方案2】:

    我遇到了类似的问题,发现以下方法可行。代码中的 'cell' 变量为 nil,因为虽然您设置了行数,但尚未创建实际的单元格对象(行 cell = UITableView(.. below)

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell : UITableViewCell
        var player : Player
    
        if self.searchDisplayController!.active {
            var searchCell: AnyObject? = self.tableView.dequeueReusableCellWithIdentifier("Cell")
    
            if searchCell == nil {
                cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "Cell")
            } else {
                cell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell
            }            
    
            player = self.filteredPlayers[indexPath.row]
        } else {
            cell = self.tableView.dequeueReusableCellWithIdentifier(TableCellNamesEnum.PLAYER_DETAIL_CELL.rawValue, forIndexPath: indexPath) as UITableViewCell
            cell.accessoryType = UITableViewCellAccessoryType.Checkmark
            player = self.selectedPlayers[indexPath.row]
        }
    
        cell.textLabel!.text = "\(player.firstName) \(player.lastName)"
    
        return cell
    }
    

    【讨论】:

    • 你为什么混合使用dequeueReusableCellWithIdentifier(_:)和dequeueReusableCellWithIdentifier(_: forIndexPath:) ?为什么要将searchCell 转换为AnyObject?。并且不需要将dequeueReusableCellWithIdentifier(_:) 的返回类型转换为UITableViewCell。
    • 主要是因为那是示例教程中用于特定 xCode 版本的代码:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多