以下this教程,我剛剛添加到我的表視圖控制器的「搜索欄和搜索顯示控制器」。 正如可以從下面的截圖中看到,該表和搜索欄被正確地加載:通過使用「小區」作爲小區重用標識符Swift/iOS 8:搜索欄提高致命錯誤:意外地發現零,同時展開一個可選值
。 不管怎麼說,有兩個問題:
2)儘快尋找我開始輸入的東西在搜索欄中(即使它是「隱藏的」),則會引發標題中提到的異常。
這裏是我的代碼中使用/ *** /是異常提高,其中線:
import UIKit
class AllTasksViewController: UITableViewController, UISearchBarDelegate, UISearchDisplayDelegate {
var allTasks = [Task]()
var taskService = TaskService()
var organizedTasks = TaskMenuItems()
var filteredTasks = [Task]()
override func viewWillAppear(animated: Bool) {
self.tabBarController?.navigationItem.setHidesBackButton(true, animated: true)
self.tabBarController?.navigationItem.rightBarButtonItem = self.editButtonItem()
if (LoggedUser.isLogged) {
self.navigationItem.setHidesBackButton(false, animated: true)
self.taskService.requestAllTasks {
(response) in
self.allTasks = self.taskService.loadTasks(response) as! [Task]
self.organizedTasks.organize(self.allTasks)
/*println(self.organizedTasks.items["Aperti"]?.count)
println(self.organizedTasks.items["Chiusi"]?.count)
println(self.organizedTasks.items["Scaduti"]?.count)
println(self.organizedTasks.items["Sospesi"]?.count)*/
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
}
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
/* Number of sections */
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return self.organizedTasks.sections.count
}
/* Number of rows for each section */
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == self.searchDisplayController!.searchResultsTableView {
return self.filteredTasks.count
}
switch section {
case 0:
return self.organizedTasks.items["Aperti"]!.count
case 1:
return self.organizedTasks.items["Chiusi"]!.count
case 2:
return self.organizedTasks.items["Scaduti"]!.count
case 3:
return self.organizedTasks.items["Sospesi"]!.count
default:
return -1
}
}
override func tableView(tableView: UITableView,
cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
l?
var selectedStatus:String
let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! UITableViewCell /***/
var task:Task
if tableView == self.searchDisplayController!.searchResultsTableView {
task = filteredTasks[indexPath.row]
cell.textLabel?.text = task.titolo
cell.detailTextLabel?.textColor = Settings.decideColor(task.priorita)
cell.detailTextLabel?.text = task.priorita
return cell
}
switch indexPath.section {
case 0:
/*let cell = tableView.dequeueReusableCellWithIdentifier("OpenTasks",
forIndexPath: indexPath) as! UITableViewCell*/
selectedStatus = "Aperti"
break
case 1:
/*let cell = tableView.dequeueReusableCellWithIdentifier("ClosedTasks",
forIndexPath: indexPath) as! UITableViewCell*/
selectedStatus = "Chiusi"
break
case 2:
/*let cell = tableView.dequeueReusableCellWithIdentifier("ExpiredTasks",
forIndexPath: indexPath) as! UITableViewCell*/
selectedStatus = "Scaduti"
break
case 3:
/*let cell = tableView.dequeueReusableCellWithIdentifier("SuspendedTasks",
forIndexPath: indexPath) as! UITableViewCell*/
selectedStatus = "Sospesi"
break
default:
/*let cell = tableView.dequeueReusableCellWithIdentifier("",
forIndexPath: indexPath) as! UITableViewCell*/
selectedStatus = ""
break
}
task = self.organizedTasks.items[selectedStatus]![indexPath.row]
cell.textLabel?.text = task.titolo
cell.detailTextLabel?.textColor = Settings.decideColor(task.priorita)
cell.detailTextLabel?.text = task.priorita
return cell
}
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
switch (section) {
case 0:
let count = self.organizedTasks.items["Aperti"]!.count
return "Aperti (\(count))"
case 1:
let count = self.organizedTasks.items["Chiusi"]!.count
return "Chiusi (\(count))"
case 2:
let count = self.organizedTasks.items["Scaduti"]!.count
return "Scaduti (\(count))"
case 3:
let count = self.organizedTasks.items["Sospesi"]!.count
return "Sospesi (\(count))"
default:
return ""
}
}
func filterContentForSearchText(searchText: String, scope:String="All") {
// Filter the array using the filter method
self.filteredTasks = self.allTasks.filter({(task: Task) -> Bool in
let categoryMatch = (scope == "All") || (task.priorita == scope)
let stringMatch = task.titolo.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
}
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
}
我不知道爲什麼這兩個事情發生。看起來它不能用「Cell」標識符創建另一個單元格,但這很奇怪,因爲當表格被加載時(不使用搜索欄)一切正常,因此標識符Cell被註冊。 拜託,你能幫忙嗎?
非常感謝您的幫助。現在它工作了!無論如何,我仍然無法使用UISearchDisplay控制器。文檔說「不幸的是,在編寫本書時,界面構建器無法創建新的UISearchController,因此您必須使用代碼創建它。」那麼我應該聲明一個'@IBOutlet'searchController,然後將它連接到故事板?此外,我應該將哪個@IBAction連接到?如果你能給我一些提示,我會非常感謝'因爲我對這一切還是很新的......無論如何,再次感謝你 –
看看這個[鏈接](https://www.veasoftware.com/教程/ 2015/6/27/uisearchcontroller-in-swift-xcode-7-ios-9-tutorial),它解釋瞭如何在iOS 9中使用UISearchController。它在代碼中進行配置,在iOS中進行設置8應該幾乎相同。 – Gabor
這裏是解決方案 –