我正在尝试在我的表格 View 上方的 View 中连接一个搜索栏:
//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
}
当按下搜索按钮时,它会向下移动 View ,使搜索栏可见。但是,当我按下搜索栏时,会弹出此错误:
*** 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,所以这就是它在错误中这样说的原因。知道发生了什么事吗?
最佳答案
您没有使用此标识符注册 tableViewCell。将 MainTableCell
放在 Storyboard 中的单元格 ID 中或您为 tableView 注册单元格类的位置(如果您以编程方式进行)。
更新:
我认为这与从 tableView 中出列单元格有关。 我猜你有标准代码:
cell = tableView.dequeueReusableCellWithIdentifier(cellId, forIndexPath: indexPath)
请尝试将其替换为:
cell = self.tableView.dequeueReusableCellWithIdentifier(cellId, forIndexPath: indexPath)
关于ios - 搜索栏不一致错误(Swift)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38726869/