故事板布局如下:
这是我的演示,我不知道错误出在哪里。
在ViewController
:
import UIKit
import Foundation
class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
@IBOutlet var searchDisplay: UISearchDisplayController!
var ctrls:[String] = ["Label","Button1-init","Button1-high","Button2-init","Button2-high","Switch"]
var ctrlsel:[String] = []
override func viewDidLoad() {
super.viewDidLoad()
self.ctrlsel = self.ctrls
self.searchDisplay.searchResultsTableView.register(UITableViewCell.self,
forCellReuseIdentifier: "SwiftCell")
self.searchDisplay.searchBar.placeholder = "input"
//self.searchDisplay.searchBar.text = "b"
self.searchDisplay.searchBar.prompt = "search"
self.searchDisplay.searchBar.searchBarStyle = UISearchBarStyle.minimal
self.searchDisplay.searchBar.showsScopeBar = true
self.searchDisplay.searchBar.scopeButtonTitles = ["all","part","high"]
}
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
return self.ctrlsel.count
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!)
-> UITableViewCell!
{
let identify:String = "SwiftCell"
let cell = self.searchDisplay.searchResultsTableView.dequeueReusableCell(
withIdentifier: identify, for: indexPath as IndexPath) as UITableViewCell
cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
cell.textLabel?.text = self.ctrlsel[indexPath.row]
return cell
}
func searchBar(searchBar: UISearchBar!, textDidChange searchText: String!) {
self.searchText = searchText
searchCtrls()
}
func searchBar(searchBar: UISearchBar!, selectedScopeButtonIndexDidChange selectedScope: Int) {
print(selectedScope)
searchCtrls();
}
var searchText:String = ""
func searchCtrls() {
if self.searchText == "" {
self.ctrlsel = self.ctrls
}
else {
var scope = self.searchDisplay.searchBar.selectedScopeButtonIndex;
self.ctrlsel = []
for ctrl in self.ctrls {
let lc = ctrl.lowercased()
if lc.hasPrefix(self.searchText) {
if (scope == 0 || (scope == 1 && lc.hasSuffix("init"))
|| (scope == 2 && lc.hasSuffix("high"))) {
self.ctrlsel.append(ctrl)
}
}
}
}
// self.searchDisplay.searchResultsTableView.reloadData()
}
}
Appdelegate 崩溃。在swift3
和Xcode 8.0
环境中。为方便获取搜索结果,请使用Search Bar and Search Display Controller in storyboard
.
当我运行我的应用程序时,它会崩溃:中的class AppDelegate: UIResponder, UIApplicationDelegate
这一行 Appdelegate.swift
。中断信息是:Thread 1: breakpoint 2.1
编辑:
设置tableView的dataSource
and之后:出现了一个奇怪的场景:the is under thedelegate
vc
searchbar
tableView