2016-11-08 80 views
1

故事板佈局如下:問:搜索欄和搜索的appDelegate顯示控制器迅速而Xcode8.0崩潰故事板

This is my storyboard layout

這是我的演示,我不知道哪裏有錯誤出來。

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中。在swift3Xcode 8.0環境中。爲了方便獲得搜索結果,請使用Search Bar and Search Display Controller in storyboard

當我運行我的應用程序,它崩潰的:class AppDelegate: UIResponder, UIApplicationDelegate此行Appdelegate.swift .The打破信息是:Thread 1: breakpoint 2.1

編輯:

dataSource和的tableView的delegatevc後:有自帶一個奇怪的場景:searchbartableView

the searchbar is under the tableView

+0

你缺少tableView和searchBar類.. – Joe

+0

你能複製並粘貼你得到的錯誤嗎? – iamyogish

+0

好吧,我看到了錯誤的截圖,一個快速的問題已經設置爲實現代碼如下爲你的ViewController的數據源和委託? – iamyogish

回答

1

我相信你已經設置了數據源和的tableview的代表不正確,請設置數據源代表無論是在如下面的截圖所示的界面生成器,

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

選擇的tableview,並設置在連接檢查器中的數據源。

或者你也可以通過代碼做到這一點,通過在viewDidLoad中()方法中設置它們。粘貼viewDidLoad中),這些線(

tableView.dataSource = self 
tableView.delegate = self 

,我可以從你已經粘貼您的代碼段看,你已經實現的UITableViewDelegate & UITableViewDataSource方法。但是你沒有提到你的類符合這些協議,所以修改你的類聲明並指定你的類符合這些協議。

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 
//Your class implementation goes here 
} 
+0

我這樣做,我又發現了一個奇怪的問題,你能看到嗎? – aircraft

+0

對不起,延遲響應,你能告訴我你面臨的是什麼奇怪的問題嗎? – iamyogish

+0

昨天我編輯了我的帖子,昨天晚上我拿了一個「搜索欄和搜索顯示控制器」的完整示例,問題在於這個問題,現在我已經得到了一個我需要的例子。 – aircraft