Mike Schmidt
Mike Schmidt

Reputation: 1065

Search Bar Inconsistency Error (Swift)?

I am trying to hook up a search bar that is inside a view above my table 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

}

It moves down the view when the search button is pressed, making the search bar visible. However, when I press the search bar, this error pops up:

*** 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'

My cell identifier for my cells is MainTableCell, so that is why it says that in the error. Any idea what is going on?

Upvotes: 0

Views: 334

Answers (1)

eMKa
eMKa

Reputation: 201

You didn't register tableViewCell with this identifier. Put MainTableCell in cell id in Storyboard or where You register cell class for tableView (if You do it programmatically).

Update:

I think it is related to dequeuing cell from tableView. I guess You have standard code:

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

Please, try this replace this with:

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

Upvotes: 1

Related Questions