表格视图中的搜索栏Swift

时间:2014-09-27 01:04:21

标签: ios swift xcode tableview searchbar

我在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。 idk为什么。整个代码在这里

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
}

}

2 个答案:

答案 0 :(得分:1)

self.searchDisplayController是零。

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

它应该是这样的:

enter image description here

要获取该图像,请右键单击.xib文件中的Search Display Controller对象。请注意,在我的图片中,“引用Outlets”在searchDisplayControllerCandySearch之间存在关联。这就是你所缺少的。

要创建连接,请在放开鼠标时从CandySearch控制器拖动到“搜索显示控制器”,您将看到:

enter image description here

点击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

答案 1 :(得分:-1)

我有一个类似的问题,发现以下工作。 '小区'你的代码中的变量是nil,因为虽然你已经设置了行数,但是还没有创建实际的单元格对象(line 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
}
相关问题