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.
Ah, I solved it by adding
lengthto mySearchBar.text;mySearchBar.text.length > 0works. I forgot to rewrite in another method, I changedmySearchBar.texttomySearchBar.text.length, that is:@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.