故事板佈局如下:問:搜索欄和搜索的appDelegate顯示控制器迅速而Xcode8.0崩潰故事板
這是我的演示,我不知道哪裏有錯誤出來。
在ViewController
:
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() {
super.viewDidLoad()
// 起始加載全部內容
self.ctrlsel = self.ctrls
// 註冊TableViewCell
self.searchDisplay.searchResultsTableView.register(UITableViewCell.self,
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
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("初級"))
|| (scope == 2 && lc.hasSuffix("高級"))) {
self.ctrlsel.append(ctrl)
}
}
}
}
// 不需要刷新Table View顯示
// self.searchDisplay.searchResultsTableView.reloadData()
}
}
崩潰在AppDelegate中。在swift3
和Xcode 8.0
環境中。爲了方便獲得搜索結果,請使用Search Bar and Search Display Controller in storyboard
。
當我運行我的應用程序,它崩潰的:class AppDelegate: UIResponder, UIApplicationDelegate
此行Appdelegate.swift
.The打破信息是:Thread 1: breakpoint 2.1
編輯:
集dataSource
和的tableView的delegate
到vc
後:有自帶一個奇怪的場景:searchbar
是tableView
你缺少tableView和searchBar類.. – Joe
你能複製並粘貼你得到的錯誤嗎? – iamyogish
好吧,我看到了錯誤的截圖,一個快速的問題已經設置爲實現代碼如下爲你的ViewController的數據源和委託? – iamyogish