2016-08-02 7 views
0

私は、テーブルビューの上にビュー内にある検索バーをフックアップしようとしています:検索ボタンが押されたときに検索バーの不一致エラー(迅速)?

//Outlet for the table search bar/controller 
@IBOutlet var searchBar: UISearchBar! 
var searchController = UISearchController(searchResultsController: nil) 
var searchBar_optional = false 

//Function for when the search button is triggered 
@IBAction func searchButtonPressed(sender: UIBarButtonItem) { 

    if(searchBar_optional == false){ 
     tableView.contentInset = UIEdgeInsets(top: barView.bounds.size.height-30, left: 0.0, bottom: 0.0, right: 0.0) 
     searchBar_optional = true 
    } 
    else if (searchBar_optional){ 
     tableView.contentInset = UIEdgeInsets(top: barView.bounds.size.height-78, left: 0.0, bottom: 0.0, right: 0.0) 
     searchBar_optional = false 
    } 

} 

var dataArray = [MainTableViewCell]() 

var filteredArray = [MainTableViewCell]() 

var shouldShowSearchResults = false 


func filterContentForSearchText(searchText: String, scope: String = "All"){ 
    filteredArray = dataArray.filter{ cell in 
     if((cell.fileName.text?.containsString(searchText.lowercaseString)) == true){ 
      return true 
     } 
     else if((cell.fileDescription.text?.containsString(searchText.lowercaseString)) == true){ 
      return true 
     } 
     else if((cell.fileCategory.text?.containsString(searchText.lowercaseString)) == true){ 
      return true 
     } 
     else if((cell.fileType.text?.containsString(searchText.lowercaseString)) == true){ 
      return true 
     } 
     else{ 
      return false 
     } 

    } 
    tableView.reloadData() 
} 

func updateSearchResultsForSearchController(searchController: UISearchController) { 
    filterContentForSearchText(searchBar.text!) 
} 

func configureSearchController() { 
searchController.searchResultsUpdater = self 
searchController.dimsBackgroundDuringPresentation = false 
definesPresentationContext = true 

} 

それは検索バーが見えるように、ビューを下に移動します。しかし、私は検索バーを押したときに、このエラーがポップアップ表示:

*** Assertion failure in -[UISearchResultsTableView 
dequeueReusableCellWithIdentifier:forIndexPath:], 
/BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit- 
3512.60.7/UITableView.m:6573 
2016-08-02 13:13:05.001 References[22478:14561725] *** Terminating app due 
to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to 
dequeue a cell with identifier MainTableCell - must register a nib or a 
class for the identifier or connect a prototype cell in a storyboard' 

私の細胞のための私のセル識別子はMainTableCellあるので、それは誤りでと言う理由です。何が起こっているか考えてみませんか?

+0

あなたがのUITableViewとあなたのnibファイルを登録しましたか? – tek3

+0

@ tek3はい。テーブルビューが正常に動作します。検索バーの内側をクリックすると唯一のエラーが発生します –

+0

このリンクが役立つかどうかを確認してください。 http://stackoverflow.com/questions/14207142/assertion-failure-when-using-uisearchdisplaycontroller-in-uitableviewcontroller – tek3

答えて

1

この識別子でtableViewCellを登録しませんでした。 Storyboardのcell idにMainTableCellを入れてください。あるいは、あなたがtableViewのセルクラスを登録する場所(プログラムで行う場合)。

更新:

私はそれがのtableViewからセルをデキューに関連していると思います。 は、私はあなたが標準のコードを持っていると思います:

cell = tableView.dequeueReusableCellWithIdentifier(cellId, forIndexPath: indexPath) 

、これはでこれを置き換えてみてください。

cell = self.tableView.dequeueReusableCellWithIdentifier(cellId, forIndexPath: indexPath) 
+0

私はそれを登録しました。エラーはテーブルビューではなく、検索バーの内側をクリックしたときです –

関連する問題