我正在尝试连接位于表格视图上方视图内的搜索栏:
//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 :(得分:1)
您没有使用此标识符注册tableViewCell。将MainTableCell
放在Storyboard中的单元格id中,或者为tableView注册单元格类(如果以编程方式执行)。
<强>更新强>
我认为它与从tableView中出列单元格有关。 我猜你有标准代码:
cell = tableView.dequeueReusableCellWithIdentifier(cellId, forIndexPath: indexPath)
请尝试将此替换为:
cell = self.tableView.dequeueReusableCellWithIdentifier(cellId, forIndexPath: indexPath)