xcode Table View Swift 中的搜索栏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26070294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Search Bar in Table View Swift
提问by patrikbelis
im doing a simple project in Xcode 6 and i want to add search bar in tableviewcontroller but something is not working for me. Im doing it by this tutorial http://www.raywenderlich.com/76519/add-table-view-search-swift
我在 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
}
}
here im getting error "fatal error: unexpectedly found nil while unwrapping an Optional value". idk why. whole code is here
在这里我收到错误“致命错误:在解开可选值时意外发现 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
}
}
}
回答by Austen Chongpison
self.searchDisplayController
is nil.
self.searchDisplayController
为零。
I just downloaded the tutorial's sample code (which you should do as well) and I see that the author has a "Search Display Controller" in their nib file. Check your nib file and be sure that the Candy Search
controller is hooked up properly.
我刚刚下载了教程的示例代码(你也应该这样做),我看到作者在他们的 nib 文件中有一个“搜索显示控制器”。检查您的 nib 文件并确保Candy Search
控制器正确连接。
It's supposed to look like this:
它应该是这样的:
To get to that image right click on the Search Display Controller
object in the .xib file. Notice in my image that "Referencing Outlets" has a connection between searchDisplayController
and CandySearch
. That's what you are missing.
要获得该图像,请右键单击Search Display Controller
.xib 文件中的对象。请注意,在我的图像中,“引用插座”在searchDisplayController
和之间有连接CandySearch
。这就是你所缺少的。
To create the connection ctrl drag from the CandySearch
controller to the `Search Display Controller" when you let go of the mouse you will see:
要创建连接,CandySearch
请在松开鼠标时将ctrl 从控制器拖动到“搜索显示控制器”,您将看到:
Click searchDisplayController
and you should be good to go.
单击searchDisplayController
,您应该可以开始使用了。
Lastly, you should read up on how optionals work in Swift to help you understand issues like this in the future:
最后,您应该阅读 Swift 中可选项的工作原理,以帮助您在将来理解这样的问题:
回答by as21
I had a similar issue and found the following to work. The 'cell' variable in your code is nil because while you have set the number of rows, the actual cell object has not yet been created (line cell = UITableView(.. below)
我有一个类似的问题,并发现以下工作。代码中的 'cell' 变量为零,因为当您设置了行数时,尚未创建实际的单元格对象(行 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
}