Reputation: 9813
I have this part of a method that is giving me an error. This is a word filter, user types a, all words come up, etc...
Error comes when user deletes the searchbar text, and clicks something on the table, I get out of bounds exception.
The print out for filteredListContent is a single "" entry, what should I implement from keeping the "" from crashing the program?
Thanks
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([_filteredListContent count]>0){
NSLog(@"%i",indexPath.row);
NSLog(@"%@",_filteredListContent);
_searchBar.text=[self.filteredListContent objectAtIndex:indexPath.row];
//Save to user default
Upvotes: 0
Views: 1287
Reputation: 47059
Following are simple example of word filter.
-(void)viewDidLoad
{
self.listOfTemArray = [[NSMutableArray alloc] init];
self.ItemOfMainArray = [[NSMutableArray alloc] initWithObject:@"/ArrayList/"];
[self.listOfTemArray addObjectsFromArray:self.ItemOfMainArray];
}
#pragma mark -
#pragma mark Table view Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [self.listOfTemArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Foobar"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Foobar"];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
cell.textLabel.textColor = [UIColor blackColor];
}
cell.textLabel.text = [self.listOfTemArray objectAtIndex: indexPath.row];
return cell;
}
#pragma mark -
#pragma mark SearchBar methods
- (void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText
{
NSString *name = @"";
NSString *firstLetter = @"";
if (self.listOfTemArray.count > 0)
[self.listOfTemArray removeAllObjects];
if ([searchText length] > 0)
{
for (int i = 0; i < [self.ItemOfMainArray count] ; i = i+1)
{
name = [self.ItemOfMainArray objectAtIndex:i];
if (name.length >= searchText.length)
{
firstLetter = [name substringWithRange:NSMakeRange(0, [searchText length])];
//NSLog(@"%@",firstLetter);
if( [firstLetter caseInsensitiveCompare:searchText] == NSOrderedSame )
{
// strings are equal except for possibly case
[self.listOfTemArray addObject: [self.ItemOfMainArray objectAtIndex:i]];
NSLog(@"=========> %@",self.listOfTemArray);
}
}
}
}
else
{
[self.listOfTemArray addObjectsFromArray:self.ItemOfMainArray ];
}
[self.tblView reloadData];
}
This code might useful in your case...
Thanks:)
Upvotes: -1
Reputation: 5128
I'm not sure what is causing your problem but from the code given it looks like the row selected is "out of bounds". I would change my check:
[_filteredListContent count] > 0
to something like:
[_filteredListContent count] > indexPath.row
Again this is just based on the code given.
Upvotes: 2
Reputation: 4805
I guess you are using search display controller for searching in table. But at the same time you are using same array reference(self.filteredListContent) for both tables(One is search results table & other one is original table that used to display all words). When you type a character in search bar you are modifying self.filteredListContent array. That's why when you search something self.filteredListContent gets modified(will may contain less objects than used to load original table) and then you select any row of original table it will crash if you try to access object at index that does not exist in array.
So I suggest you to keep two arrays. One for loading original table & one for search results table
Upvotes: 1