2016-08-02 78 views
0

我试图挂钩一个搜索栏,上面是我的表视图中的视图中:搜索栏不一致错误(Swift)?

//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笔尖文件:

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

请试试这个用替换此? – tek3

+0

@ tek3是的。表格视图工作正常,当我在搜索栏内点击时发生唯一的错误 –

+0

看看这个链接是否有帮助。 http://stackoverflow.com/questions/14207142/assertion-failure-when-using-uisearchdisplaycontroller-in-uitableviewcontroller – tek3

回答

1

您没有使用此标识符注册tableViewCell。将MainTableCell放置在Storyboard中的单元标识中,或者您为TableView注册单元类(如果以编程方式执行)。

更新:

我认为这是关系到从的tableView离队细胞。 我猜你有标准代码:

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

我注册了它。该错误不是与表格视图,而是当我在搜索栏内单击时 –