I have an app with a tableview which you can add and delete items though when I tried to implement a search bar it crashes whenever I type one letter. Here is the code that I am using:
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
if (searchText.length == 0) {
isFiltered = NO;
} else {
isFiltered = YES;
filteredPatients = [[NSMutableArray alloc] init];
for (Patient *patient in patients) {
NSRange patientNameRange = [patient.patientName rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (patientNameRange.location != NSNotFound) {
[filteredPatients addObject:patient];
}
}
}
[self.tableView reloadData];
}
This works fine though when you type a letter which has a patient in then it breaks at this line:
cell.textLabel.text = [filteredPatients objectAtIndex:indexPath.row];
Here is the code in context:
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:#"cell"];
if ( nil == cell ) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
}
NSLog(#"indexPath.row = %d, patients.count = %d", indexPath.row, patients.count);
Patient *thisPatient = [patients objectAtIndex:indexPath.row];
if (isFiltered == YES) {
cell.textLabel.text = [filteredPatients objectAtIndex:indexPath.row];
} else {
cell.textLabel.text = [NSString stringWithFormat:#"%# %#", thisPatient.patientName, thisPatient.patientSurname];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.textColor = [UIColor blackColor];
if (self.editing) {
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
return cell;
}
And returns this error
-[Patient isEqualToString:]: unrecognized selector sent to instance 0x756c180
If you want any more code then ask.
Thanks in advance
You are iterating on the collection patients that seems to contain Patient instances and not NSString instances. So I would do something like :
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
if (searchText.length == 0) {
isFiltered = NO;
} else {
isFiltered = YES;
filteredPatients = [[NSMutableArray alloc] init];
for (Patient *patient in patients) {
NSRange patientNameRange = [patient.name rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (patientNameRange.location != NSNotFound) {
[filteredPatients addObject:patient];
}
}
}
[self.tableView reloadData];
}
Related
I have implemented a TableView with custom cells with two labels to populate city name and city id (I kept the city id label hidden). Here my problem is when I search for the city name I cannot get the city id also, when I search the city name, I want both the values to be filtered.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellidentifier=#"citylocation";
searchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellidentifier];
if (!cell) {
cell= [[searchTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentifier];
}
if(!isFilltered)
{
cell.textLabel.text = [avahotel objectAtIndex:indexPath.row];
cell.city.text = [[createdDate objectAtIndex:indexPath.row]stringValue];
}else
{
cell.textLabel.text = [filteredString objectAtIndex:indexPath.row];
}
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(isFilltered)
{
return [filteredString count];
}
return [avahotel count];
}
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if(searchText.length == 0)
{
isFilltered = NO;
}else
{
isFilltered = YES;
filteredString = [[NSMutableArray alloc]init];
for(NSString *str in avahotel)
{
NSRange stringRange = [str rangeOfString:searchText options:NSCaseInsensitiveSearch];
if(stringRange.location != NSNotFound)
{
[filteredString addObject:str];
}
}
}
[_searchtableview reloadData];
}
Use following code in else part for this method. searchBar textDidChange
Updated
isFilltered = YES;
filteredString = [[NSMutableArray alloc]init];
filteredCityId = [[NSMutableArray alloc]init];
for(Int i=0; i<avahotel.count; i++)
{
NSString *str = [avahotel objectAtIndex:i];
NSRange stringRange = [str rangeOfString:searchText options:NSCaseInsensitiveSearch];
if(stringRange.location != NSNotFound)
{
[filteredString addObject:[avahotel objectAtIndex:i]];
NSString *strId = [NSString stringWithFormat:#"%d",i];
[filteredCityId addObject:strId]
// here your both filter array declare.filteredcityId and filteredString
}
}
I have the following search setup in my app:
- (UISearchBar*)searchBar
{
if (!_searchBar) {
_searchBar = [[UISearchBar alloc] init];
_searchBar.delegate = self;
_searchBar.placeholder = kSearchBarPlaceHolder;
}
return _searchBar;
}
- (UISearchDisplayController*)searchBarDisplayContr
{
if (!_searchBarDisplayContr) {
_searchBarDisplayContr = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
_searchBarDisplayContr.delegate = self;
_searchBarDisplayContr.searchResultsDataSource = self.tableView.dataSource;
_searchBarDisplayContr.searchResultsDelegate = self.tableView.delegate;
_searchBarDisplayContr.searchResultsTableView.backgroundColor = [UIColor clearColor];
_searchBarDisplayContr.searchResultsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
return _searchBarDisplayContr;
}
- (NSMutableArray *)searchResults
{
if (!_searchResults) {
_searchResults = [NSMutableArray arrayWithCapacity:_restaurants.count];
}
return _searchResults;
}
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
[self.searchResults removeAllObjects];
[self.restaurants enumerateObjectsUsingBlock:^(Restaurant *restaurant, NSUInteger idx, BOOL *stop) {
if ([scope isEqualToString:#"All"] || [restaurant.name isEqualToString:scope]) {
NSRange range = [restaurant.name rangeOfString:searchText
options:(NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch)];
if (range.length > 0) {
[self.searchResults addObject:restaurant];
}
}
}];
}
- (BOOL)searchDisplayController:(UISearchDisplayController*)controller shouldReloadTableForSearchString:(NSString*)searchString
{
[self filterContentForSearchText:searchString
scope:#"All"];
dispatch_async(dispatch_get_main_queue(), ^(void) {
for (UIView *v in controller.searchResultsTableView.subviews) {
if ([v isKindOfClass:[UILabel class]]) {
((UILabel *)v).text = kSearchResultsTableViewNoResultsLabel;
((UILabel *)v).font = [UIFont mediumFontOfSize:20.0f];
((UILabel *)v).textColor = [UIColor blackColor];
break;
}
}
});
return YES;
}
- (BOOL)searchDisplayController:(UISearchDisplayController*)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
[self filterContentForSearchText:[self.searchBarDisplayContr.searchBar text]
scope:#"All"];
return YES;
}
But for some reason when I search the searchResultsTableView is not updated / cellForRowAtIndexPath is not called. The tableView delegates and DataSource is setup in my storyboard.
Any ideas why this is happening?
UPDATE:
[self.tableView registerClass:[RestaurantsCell class] forCellReuseIdentifier:cellIdentifier];
[self.searchBarDisplayContr.searchResultsTableView registerClass:[RestaurantsCell class] forCellReuseIdentifier:cellIdentifier];
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchBarDisplayContr.searchResultsTableView) {
return [self.searchResults count];
} else {
return [self.restaurants count];
}
return 0;
}
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
RestaurantsCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
Restaurant *restaurant;
if (tableView == self.searchBarDisplayContr.searchResultsTableView) {
if (self.searchResults.count > 0) {
restaurant = self.searchResults[indexPath.row];
}
} else {
if (self.restaurants.count > 0) {
restaurant = self.restaurants[indexPath.row];
}
}
if (restaurant) {
cell.titleLabel.text = restaurant.name;
}
return cell;
}
This is the offending line:
RestaurantsCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
Should be:
RestaurantsCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];
Explanation:
If the tableView turns out to be your search results table view, then trying to dequeue a cell with an identifier won't work because it doesn't have prototype cells. Therefore, you have to use self.tableView
Also, your code can be cleaned up a lot:
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
return (tableView == self.tableView) ? self.restaurants.count : self.searchResults.count;
}
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
RestaurantsCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];
Restaurant *restaurant = (tableView == self.tableView) ? self.restaurants[indexPath.row] ? self.searchResults[indexPath.row];
}
cell.titleLabel.text = restaurant.name;
return cell;
}
Edit
Check out this example
I need to set a tag for the button created on the table view cell.
I implemented the UISearchBar and it works well.
Issue is after filtering the tableView cell, its indexpath.row changes as I am setting
'button.tag = indexPath.row'.
Is there a way to keep the row number constant for every cell?
Or is there any other solution?
Note: I have multiple sections in one UITableView and isFiltered is BOOL value which indicates user have started to type text in UISearchBar.
Implemented Searching with help of https://github.com/kwylez/IndexedTable
In cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId = #"CheckBoxedCell";
// NSString *cellId = [NSString stringWithFormat:#"Section:%d Row:%d",indexPath.section,indexPath.row];
CheckBoxedCellClass *cell = (CheckBoxedCellClass *)[self.tableViewContact dequeueReusableCellWithIdentifier:cellId];
if(!cell)
{
NSArray *nib;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
nib = [[NSBundle mainBundle] loadNibNamed:#"CheckBoxedCellClass" owner:self options:nil];
}
else if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
nib = [[NSBundle mainBundle] loadNibNamed:#"CheckBoxedCellClass_iPad" owner:self options:nil];
}
for (id object in nib)
{
if([object isKindOfClass:[CheckBoxedCellClass class]])
{
cell = (CheckBoxedCellClass *)object;
break;
}
}
cell = [nib objectAtIndex:0];
}
SaveCheckBoxedView *saveContact;
if(isFiltered == YES)
{
saveContact = [filterdArray objectAtIndex:indexPath.row];
cell.nameLabel.text = saveContact.nameString;
}
else
{
saveContact = [mutableArray objectAtIndex:indexPath.row];
cell.nameLabel.text = [[objectsForCharacters objectForKey:[arrayOfCharacters objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
}
//cell.nameLabel.text = saveContact.nameString;
cell.companyLabel.text = saveContact.companyString;
cell.invIdLabel.text = [NSString stringWithFormat:#"%d", saveContact.invitId];
//set fonts
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
[cell.companyLabel setFont:[UIFont italicSystemFontOfSize:10.0]];
}
else
{
[cell.companyLabel setFont:[UIFont italicSystemFontOfSize:14.0]];
}
//handling check box
NSInteger rowNumber = 0;
for(NSInteger i = 0; i < indexPath.section ; i++)
{
rowNumber += [self tableView:self.tableViewContact numberOfRowsInSection:i];
}
rowNumber += indexPath.row;
/*if([indexPath compare:self.lastIndexPath] == NSOrderedSame)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
NSString *finalIntId = [mutableArrayOfIds objectAtIndex:rowNumber];
NSLog(#"Tagged checked button id = %#", finalIntId);
[arrayOfIds addObject:finalIntId];
}*/
UIButton *checkBox;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
checkBox = [[UIButton alloc]initWithFrame:CGRectMake(7, 8, 30, 30)];
}
else
{
checkBox = [[UIButton alloc]initWithFrame:CGRectMake(15, 13, 30, 30)];
}
[checkBox setImage:[UIImage imageNamed:#"checkBox.png"] forState:UIControlStateNormal];
[checkBox addTarget:self action:#selector(checkBoxClicked:event:) forControlEvents:UIControlEventTouchUpInside];
if(isFiltered == YES)
{
checkBox.tag = ;
}
else
{
checkBox.tag = rowNumber;
}
[cell.contentView addSubview:checkBox];
return cell;
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
if(isFiltered == YES) {
return Nil;
} else {
NSArray *toBeReturned = [NSArray arrayWithArray:
[#"A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|#"
componentsSeparatedByString:#"|"]];
return toBeReturned;
}
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
if (title == UITableViewIndexSearch) {
CGRect searchBarFrame = self.searchDisplayController.searchBar.frame;
[tableView scrollRectToVisible:searchBarFrame animated:YES];
return -1;
} else {
NSInteger count = 0;
for (NSString *character in arrayOfCharacters) {
if ([character isEqualToString:title]) {
return count;
}
count ++;
}
return 0;
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if(isFiltered == YES) {
return 1;
} else {
return [arrayOfCharacters count];
//return 1;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(isFiltered == YES) {
return [filterdArray count];
} else {
//return [mutableArray count];
return [[objectsForCharacters objectForKey:[arrayOfCharacters objectAtIndex:section]] count];
}
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if ([arrayOfCharacters count] == 0) {
return #"";
}
return [NSString stringWithFormat:#"%#", [arrayOfCharacters objectAtIndex:section]];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if(searchText.length == 0)
{
isFiltered = NO;
}
else
{
isFiltered = YES;
filterdArray = [[NSMutableArray alloc] init];
for (SaveCheckBoxedView *contact in mutableArray)
{
NSRange nameRange = [contact.nameString rangeOfString:searchText options:NSCaseInsensitiveSearch];
if(nameRange.location != NSNotFound)
{
[filterdArray addObject:contact];
}
}
}
[self.tableViewContact reloadData];
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[filterdArray removeAllObjects];
if(searchString.length > 0)
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF contains [search] %#", self.mySearchBar.text];
for (NSString *key in arrayOfCharacters)
{
NSArray *matches = [objectsForCharacters[key] filteredArrayUsingPredicate:predicate];
[filterdArray addObjectsFromArray:matches];
}
}
return YES;
}
I am describing one way to do so, you may find better way.
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[filterdArray removeAllObjects];
if(searchString.length > 0)
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF contains [search] %#", self.mySearchBar.text];
for (NSString *key in arrayOfCharacters)
{
NSArray *matches = [objectsForCharacters[key] filteredArrayUsingPredicate:predicate];
/****see bellow****/
[filterdArray addObjectsFromArray:matches];
}
}
return YES;
}
Try to find the row number of each object of "matches". that will be the tag of your buttons. Make filterdArray an array of dictionary. Add 2 field to dictionary. one for tag another for value. in cellForRowAtIndexPath.
if(isFiltered == YES)
{
checkBox.tag = [filterdArray objectForKey: #"tag"] ;
}
else
{
checkBox.tag = rowNumber;
}
Edited code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil)
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
if (isFiltered) {
int rowCount=indexPath.row;
Aves *filtrada=[filteredTableData objectAtIndex:rowCount];
cell.textLabel.text=filtrada.name;
NSLog(#"mostrando: ");
}else {
int rowCounter=indexPath.row;
Aves *author=[theauthors objectAtIndex:rowCounter];
cell.textLabel.text=author.name;
}
NSLog(#"mostrando: ");
return cell;
}
-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text
{
if(text.length == 0)
{
isFiltered = FALSE;
}
else
{
isFiltered = true;
int i;
[filteredTableData removeAllObjects];
for(i=0;[theauthors count]>i;i++)
{
Aves *name=[theauthors objectAtIndex:i];
//NSLog(name.name);
NSRange nameRange = [[name.name lowercaseString] rangeOfString:[text lowercaseString]];
if(nameRange.length>0)
{
[filteredTableData addObject:name];
NSLog(name.name);
}
}
[self.tableView performSelectorOnMainThread:#selector(reloadData) withObject:nil waitUntilDone:NO];
}
}
Edit: After working on it a while I solved some problems.Just updated my code, the problem is the repaint of the tableView, every thing else go ok. Check it and give any ideas you have plz ^^
Thx again for your time.
I assume you're using prototype cells? I just had a similar problem in one of my projects.
When search results are displayed and tableView:cellForRowAtIndexPath: is called, the table view passed in the the table belonging to the search results controller, not your main table view. Problem with that is, the search results table view doesn't know anything about your table's prototype cells, so dequeueReusableCellWithIdentifier: returns nil. But just alloc/init'ing a UITableCellView won't give you one of your prototype cells, so whatever UI you laid out in your storyboard isn't there.
The fix is easy: in tableView:cellForRowAtIndexPath:, don't call dequeueReusableCellWithIdentifier: on the tableview passed in; just call it on your main table view. So basically, just change this:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil)
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
to this:
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
There's no need for the nil check; Apple's Storyboards Release Notes says:
The dequeueReusableCellWithIdentifier: method is guaranteed to return
a cell (provided that you have defined a cell with the given
identifier). Thus there is no need to use the “check the return value
of the method” pattern as was the case in the previous typical
implementation of tableView:cellForRowAtIndexPath:.
does your app hits this code if(nameRange.location ==0) ?
Change the code piece of
else
{
isFiltered = true;
int i=0;
[filteredTableData removeAllObjects];
filteredTableData = [[NSMutableArray alloc] init];
//for (Aves* name in theauthors)
for(i=0;[theauthors count]>i;i++)
{
Aves *name=[theauthors objectAtIndex:i];
NSRange nameRange = [name.foodName rangeOfString:text ];
if(nameRange.location ==0)
{
[filteredTableData addObject:name];
}
[self.tableView reloadData];
}
}
to
else
{
isFiltered = true;
int i=0;
[filteredTableData removeAllObjects];
//filteredTableData = [[NSMutableArray alloc] init]; not needed
//for (Aves* name in theauthors)
for(i=0;[theauthors count]>i;i++)
{
Aves *name=[theauthors objectAtIndex:i];
NSRange nameRange = [name.foodName rangeOfString:text ];
if(nameRange.location != NSNotFound)
{
[filteredTableData addObject:name];
}
}
[self.tableView reloadData];
}
use this:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//If the requesting table view is the search display controller's table view, return the count of the filtered list, otherwise return the count of the main list.
if (isFiltered)
{
return [filteredTableData count];
}
else
{
return [theauthors count];
}
}
Replace method:
-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text
{
if(text.length == 0)
{
isFiltered = FALSE;
}
else
{
isFiltered = true;
int i=0;
[filteredTableData removeAllObjects];
filteredTableData = [[NSMutableArray alloc] init];
//for (Aves* name in theauthors)
for(i=0;[theauthors count]>i;i++)
{
Aves *name=[theauthors objectAtIndex:i];
NSRange nameRange = [[name.foodName lowercaseString] rangeOfString:[text lowercaseString]];
if(nameRange.length>0)
{
[filteredTableData addObject:name];
[self.tableView reloadData];
}
}
}
}
finally fixed it. Here is my working code, thx you all =)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
if (isFiltered==TRUE) {
int rowCount=indexPath.row;
//for (rowCount=0; rowCount<[filteredTableData count]; rowCount++) {
Aves *filtrada=[filteredTableData objectAtIndex:rowCount];
cell.textLabel.text=filtrada.name;
//}
}else if(isFiltered==FALSE)
{
int rowCounter=indexPath.row;
Aves *author=[theauthors objectAtIndex:rowCounter];
cell.textLabel.text=author.name;
}
return cell;
}
-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text {
[filteredTableData removeAllObjects];
filteredTableData=[[NSMutableArray alloc]init ];
if(text.length == 0)
{
isFiltered = FALSE;
}
else
{
isFiltered = TRUE;
int i;
for(i=0;[theauthors count]>i;i++)
{
Aves * filtrado=[[Aves alloc]init];
filtrado=[theauthors objectAtIndex:i];
//NSLog(filtrado.name);
NSRange nameRange = [[filtrado.name lowercaseString] rangeOfString:[text lowercaseString]];
if(nameRange.length>0)
{
[filteredTableData addObject:filtrado];
}
}
[self.tableView performSelectorOnMainThread:#selector(reloadData) withObject:nil
waitUntilDone:NO];
}
}
You probably want to replace
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
with in your cellForRowAtIndexPath
UITableViewCell *cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
The reason because you're not getting use of the dequeued cell anyway.
Here is my problem, I cant seem to get past typing in 2 characters in the search bar with out the app crashing on me. The first one works as it should but when I search for any other butterfly other than the first one CRASH! Please help!
[filteredComList removeAllObjects];
[filteredImgList removeAllObjects];
[filteredSciList removeAllObjects];
[filteredComList addObjectsFromArray:buttComNameList];
[filteredSciList addObjectsFromArray:buttSciNameList];
[filteredImgList addObjectsFromArray:butterflyImages];
}else {
[filteredComList removeAllObjects];
[filteredImgList removeAllObjects];
[filteredSciList removeAllObjects];
for (NSString *string in buttComNameList) {
NSRange range = [string rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (range.location != NSNotFound) {
[filteredComList addObject:string];
}
}
for (NSString *string in buttSciNameList) {
NSRange range = [string rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (range.location != NSNotFound) {
[filteredSciList addObject:string];
}
}
for (NSString *string in butterflyImages) {
NSRange range = [string rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (range.location != NSNotFound) {
[filteredImgList addObject:string];
}
}
}
[myButterflyTable reloadData];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
_searchWasActive = NO;
[butterflySearchBar resignFirstResponder];
[self.tableView setFrame:self.view.bounds];
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
_searchWasActive = YES;
NSLog(#"began editing");
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
_searchWasActive = NO;
NSLog(#"ended editing");
}
Table delegate methods below
- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section
return [filteredComList count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
Defenitions *def = [[Defenitions alloc]init];
// Configure the cell...
//[self fetchedResultsController:[self fetchedResultsControllerForTableView:tableView] configureCell:cell atIndexPath:indexPath];
if (_searchWasActive) {
[def defineButterfly:[filteredComList objectAtIndex:indexPath.row] defineLatin: [filteredSciList objectAtIndex:indexPath.row] setImage:[filteredImgList objectAtIndex:indexPath.row]];
}else {
[def defineButterfly:[buttComNameList objectAtIndex:indexPath.row] defineLatin:[buttSciNameList objectAtIndex:indexPath.row] setImage:[butterflyImages objectAtIndex:indexPath.row]];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
[cell.contentView addSubview:[def commonName]];
[cell.contentView addSubview:[def latinName]];
[cell.contentView addSubview:[def butterflyImage]];
return cell;
}