即時通訊在Xcode 6中做一個簡單的項目,我想在tableviewcontroller中添加搜索欄,但有些東西不適合我。林本教程做 http://www.raywenderlich.com/76519/add-table-view-search-swift在表視圖中的搜索欄Swift
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == self.searchDisplayController!.searchResultsTableView {
return self.filteredProgramy.count
} else {
return self.programy.count
}
}
這裏即時得到錯誤「致命錯誤:意外發現零而展開的可選值」。 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
}
}
該錯誤無關特別是在搜索欄中做什麼,並且要了解如何正確使用optionals,你會得到哪一行錯誤?你可能會強制打開一個可選值並得到nil(如錯誤信息所述) – 2014-09-27 01:16:15
這裏。「如果tableView == self.searchDisplayController!.searchResultsTableView {」 – patrikbelis 2014-09-28 17:42:10
你有沒有得到這個整理? – 2014-09-29 20:44:47