wagashi
wagashi

Reputation: 894

iPhone cellforrowatindexpath issue two nsarray

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:

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

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

It should be

.

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:

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.

Upvotes: 1

Views: 305

Answers (3)

wagashi
wagashi

Reputation: 894

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.

Upvotes: 0

SVD
SVD

Reputation: 4753

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.

Upvotes: 0

ColdLogic
ColdLogic

Reputation: 7275

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.

Upvotes: 1

Related Questions