aircraft aircraft - 1 year ago 349
Swift Question

Q: Search Bar and Search Display Controller in storyboard with swift and Xcode8.0 crash at Appdelegate

Storyboard layout is below:

This is my storyboard layout

This is my demo, and I don't know where the error comes out.



import UIKit
import Foundation

class ViewController: UIViewController {

@IBOutlet weak var tableView: UITableView!

@IBOutlet var searchDisplay: UISearchDisplayController!

// 所有组件
var ctrls:[String] = ["Label","Button1-初级","Button1-高级","Button2-初级","Button2-高级","Switch"]

// 搜索匹配的结果,Table View使用这个数组作为datasource
var ctrlsel:[String] = []

override func viewDidLoad() {

// 起始加载全部内容
self.ctrlsel = self.ctrls
// 注册TableViewCell
forCellReuseIdentifier: "SwiftCell")
// 设置文本提示
self.searchDisplay.searchBar.placeholder = "输入搜索信息"
// 可以设置初始值
//self.searchDisplay.searchBar.text = "b"
// 设置搜索栏提示信息
self.searchDisplay.searchBar.prompt = "搜索组件名称"
// 不显示Search Bar边框
self.searchDisplay.searchBar.searchBarStyle = UISearchBarStyle.minimal
// 显示分段条
self.searchDisplay.searchBar.showsScopeBar = true
self.searchDisplay.searchBar.scopeButtonTitles = ["全部","初级","高级"]

// 返回表格行数(也就是返回控件数)
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
return self.ctrlsel.count

// 创建各单元显示内容(创建参数indexPath指定的单元)
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

// 搜索代理UISearchBarDelegate方法,每次改变搜索内容时都会调用
func searchBar(searchBar: UISearchBar!, textDidChange searchText: String!) {
self.searchText = searchText

// 选择分段条时调用
func searchBar(searchBar: UISearchBar!, selectedScopeButtonIndexDidChange selectedScope: Int) {
// 保存搜索内容
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("初级"))
|| (scope == 2 && lc.hasSuffix("高级"))) {

// 不需要刷新Table View显示
// self.searchDisplay.searchResultsTableView.reloadData()


Crash at Appdelegate. In the
Xcode 8.0
environment. To convenient get search result ,use
Search Bar and Search Display Controller in storyboard

When I run my application, it crash at :
class AppDelegate: UIResponder, UIApplicationDelegate
this line in
.The break info is :
Thread 1: breakpoint 2.1


after set
of tableView to
: there comes a strange scenario:the
is under the

the searchbar is under the tableView

Answer Source

I believe you have set the datasource and the delegate of the tableview incorrectly, please set the datasource and delegate either in the interface builder as shown in the below screenshot,

Select the tableview and set the data source in the connection inspector

Select the tableview and set the data source in the connection inspector.

Or you can do this via code, by setting them in your viewDidLoad() method. Paste these lines in viewDidLoad()

tableView.dataSource = self
tableView.delegate = self

As I can see from your code snippet that you've pasted, you've already implemented the UITableViewDelegate & UITableViewDataSource methods. But you've not mentioned that your class conforms to these protocols so modify your class declaration and specify that your class conforms to these protocols.

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
//Your class implementation goes here
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download