1
votes

Here is my code below:

- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        
    NSInteger row = [indexPath row];
    NSString *contentForThisRow = nil;
    NSString *contentForThisRow2 = nil;
    
    if (mySearchBar.text > 0)
    {
        contentForThisRow = [self.filteredListContent objectAtIndex:row];
        NSInteger noWordIndex = [self.noWords indexOfObject:contentForThisRow];
        contentForThisRow2 = [self.enWords objectAtIndex:noWordIndex];
            NSLog (@"if success?");     
    }
        else 
    {
        contentForThisRow = [self.noWords objectAtIndex:row] ;
        contentForThisRow2 = [self.enWords objectAtIndex:row];
        NSLog (@"else success?");
    }
    
    static NSString *kCellID = @"cellID";

//standard code here etc for this method..

}

The codes above work perfectly except whenever I have used searchBar to filter and then click on Cancel button in the searchBar or Search button in the keyboard and then when I click on my custom "change" button in the navigationbar, the app crashes.

Before I use searchBar, there show up 4 NSLog after each change like:

  • 2011-08-15 17:21:24.481 Enne1[4750:207] else success?
  • 2011-08-15 17:21:24.483 Enne1[4750:207] else success?
  • 2011-08-15 17:21:24.484 Enne1[4750:207] else success?
  • 2011-08-15 17:21:24.485 Enne1[4750:207] else success?

And when I use searchBar to filter words, there show up also 4 NSLog like this:

  • 2011-08-15 17:19:33.713 E1[4744:207] if success?
  • 2011-08-15 17:19:33.714 E1[4744:207] if success?
  • 2011-08-15 17:19:33.714 E1[4744:207] if success?
  • 2011-08-15 17:19:33.715 E1[4744:207] if success?

But when after I have used searchBar and then cleared the searchText either with Cancel or Search and then click on "change button", there show up only 1 NSLog like this: 2

  • 011-08-15 17:21:49.806 E1[4750:207] if success?

It should be

  • else success

    in order to show the full lists, not

  • if success

.

Am I missing something?

EDIT 15 august: I have tried

if(mySearchBar.text.length > 0)

as well, but the tableview shows nothing when I clear my search string and there came up only 2 nslogs, that is:

  • 2011-08-15 23:49:06.624 E1[5064:207] if success?
  • 2011-08-15 23:49:06.626 E1[5064:207] if success?

By the way, why does it show up 4 nslogs each time I enter one alphabet in the search bar? Shouldnt it show only one nslog each time?

And my codes for textDidChange is:

 - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchString
    
    {
NSLog (@" ss: %@", searchString);
        if ([searchString length] == 0) {
            [self performSelector:@selector(hideKeyboardWithSearchBar:) withObject:searchBar afterDelay:0];
            NSLog (@" searchstring: %@", searchString);
        }   
         [self filterContentForSearchText:searchString]; 
        [tableView reloadData];
        NSLog (@"has reloaded!");
        
        return;
    }

Edit 15 august; This is wrong: I suspect the code above is causing the app crashing? not reloading tableview properly? Am I right? NSLog for searchString showed nothing...

2nd edit 15 august: I added NSLog (@" ss: %@", searchString); and of course it shows alphabet(s) each time I enter one alphabet. So it must be something wrong with mySearchBar.text > 0, how should I write this properly?

By the way, I added tableview and searchbar programmatically, tableviews delegate and datasource is linked to self and searchbars delegate is linked to self as well. There is nothing in InterfaceBuilder, only UIView.

3
Try printing mySearchBar.text as well. Is your search bar attached to the mySearchBar outlet by the way? Do you have UISearchBarDelegate implemented and assigned to the search bar's delegate property? (You probably need to reload the table in searchBar:textDidChange:)SVD
Oh, and some information about the crash (i.e. where does it crash and what is the reason) could be useful to.SVD
Not sure it has anything to do with it, but you shouldn't say if (mySearchBar.text > 0). You're comparing (what I presume to be) a pointer to zero for greater-than/less-than, and the pointer may have a high bit set that makes it appear to be negative.Hot Licks
@SVD: *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSArray objectAtIndex:]: index 2147483647 beyond bounds [0 .. 216894]' and terminate called after throwing an instance of 'NSException'. This happens when I click on a navigationButton where I change source to cell with another nsarray. When I open app and the change works perfectly between two nsarrays, but after using searchbar and then clearing up text in searchbar and then clicking on the changebutton, this happens.wagashi

3 Answers

1
votes

Not really sure what you're attempting with

if(mySearch.text > 0) {
    //stuff
}

Looks, like you're trying to compare the length to see if the string is empty. Try using this instead:

if([mySearchBar text] == nil || ![[mySearchBar text] isEqualToString:@""]) {
    //stuff
}

Getting into this code block is probably what the problem is. Not sure how your objects are implemented, but if the filtered list is nil, then you would crash trying to get objects from it and what not.

0
votes

You should definitely use if(mySearchBar.text.length > 0), not if(mySearchBar.text > 0).

It probably crashes here:

contentForThisRow2 = [self.enWords objectAtIndex:noWordIndex];

because noWordIndex was -1 (i.e. 2147483647) in the previous line. It'll crash this way even if you just type in a word that doesn't exist in the noWords array, so you need to check if noWordIndex is >= 0 before using it to access enWords. This will probably fix the problem with cleaning the search text, too.

By the way, a much faster way to look up words would be using an NSDictionary instead of two arrays.

0
votes

Ah, I solved it by adding length to mySearchBar.text; mySearchBar.text.length > 0 works. I forgot to rewrite in another method, I changed mySearchBar.text to mySearchBar.text.length, that is:

- (NSInteger)tableView:(UITableView *)tableView1 numberOfRowsInSection:(NSInteger)section
{

    tableView1.rowHeight = 100 ;
    tableView1.separatorColor = [UIColor colorWithRed:0.40 green:0.70 blue:0.45 alpha:1.0];
    tableView1.opaque = NO;

    if (mySearchBar.text.length > 0)
    {
        return [self.filteredListContent count];
        NSLog (@"if return");
    }
    else
    {
        return [self.noWords count];
        NSLog (@"else return");
    }

}

@Daniel R Hicks and @ColdLogic: So both you are right that it is wrong to use only mySearchBar.text. Thank you very much for pointing me in the right direction.

But I still wonder why there come up 4 nslogs each time...


EDIT 16 august:

4 nslogs show up every time I launch the app, because there are 4 visible cells. My tableview.height is 100, so when I changed it to 50, 8 nslogs show up and as well as 8 visible cells.