user5886755
user5886755

Reputation:

While UISearchBar is active,scrolling to UITableView app get crashes

My app works fine, but suddenly when I click on UISearchBar and without typing when I scroll down UITableView then app crashes with the following error: Thread1 : EXC_BAD_INSTRUCTION

But when I click on UISearchBar and type some text, after that when I scroll down UITableView the app works fine, it does not crash.

Finally when I click on UISearchBar and type some text and remove all that text, after that when I scroll down UITableView the app works fine, it does not crash.

Error occurs in this function :

 func tableView(historyTableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = historyTableView.dequeueReusableCellWithIdentifier("cell")! as UITableViewCell;
        //self.historyTableView.scrollEnabled = true
        if(searchActive){
//...Error occurs over here ==>> Thread1 : EXC_BAD_INSTRUCTION
            cell.textLabel?.text = filtered[indexPath.row]  
        } else {
            cell.textLabel?.text = data[indexPath.row]
        }

        return cell;
    }

Upvotes: 2

Views: 408

Answers (3)

user5886755
user5886755

Reputation:

By this way I've overcome from this problem.

func tableView(historyTableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = historyTableView.dequeueReusableCellWithIdentifier("cell")! as UITableViewCell;
        if(searchActive){
            if(searchBar.text=="")
            {
                //do nothing

            }else{
                cell.textLabel?.text = filtered[indexPath.row]
            }

        } else {
            cell.textLabel?.text = data[indexPath.row]
        }

        return cell;
    }

Upvotes: 0

Sohel L.
Sohel L.

Reputation: 9540

I hope you have handled you filtered and data array in numberOfRowsInSection method too.

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if(searchActive){

        return filtered.count

    }else{

        return data.count
    }
}

Now you have to just make your searchActive to true when you click on UISearchBar.

func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
    searchActive = true;
} 

Hope this helps!

Upvotes: 0

Fennelouski
Fennelouski

Reputation: 2431

My guess is that when you begin your active search, you are not reloading the table. I'm also guessing that nunberOfRowsInSection: doesn't handle the case of an active search. If those things are true, then calling filtered[indexPath.row] will crash your app anytime you scroll to a row greater than filtered.count

Upvotes: 1

Related Questions