How can I implement search Objective-c?
I have a record like this.
FirstName - LastName - PhoneNo - Address - Designation
A - Mick - 789367789 - New york - Professor
B - Jossef - 534647458 - USA - Doctor
C - Sha - 342576765 - USA - Doctor
D - Gee - 535346457 - USA - Business Man
......Like this
I am displaying one FirstName in UITableView with UISearchBar. Search is working fine it filters data according the keyword your typing in the SearchBar but suppose you are typing M it list all the items of M and after then when i click on the first item of M, it displays the Details of A rather then M on the next View.
I think you can understand my problem.
How can i resolve this how to pass multiple values in the Next view corresponding to record.?
Thanks,
This function i took from the same code of TableView(apple sample code with searchbar ). and i modified this according to my logic.
- (void) searchTableView
{
NSString *searchText = searchBar.text;
NSMutableArray *searchArray = [[NSMutableArray alloc] init];
NSInteger TotalNoOfRecords=[self.SearchtableDataSource count];
for (int i=0;i<TotalNoOfRecords;i++)
{ NSDictionary *dictionary = [self.SearchtableDataSource objectAtIndex:i];
NSArray *array = [dictionary objectForKey:@"Title"];
NSString *arrayID= [dictionary objectForKey:@"ID"];
NSLog(@"Testing - Id-%d",arrayID);
[searchArrayID addObject:arrayID];
[searchArray addObject:array];
}
for (NSString *sTemp in searchArray)
{
NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (titleResultsRange.length > 0)
{
[copyListOfItems addObject:sTemp];
}
}
[searchArray release];
searchArray = nil;
}
This is my didSelectRowAtIndexPath code i know this need modification but i don't know how?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *dictionary = [self.SearchtableDataSource objectAtIndex:indexPath.row];
FirstName= [dictionary objectForKey:@"FirstName"];
LastName=[dictionary objectForKey:@"LastName"];
DetailViewController *ivc = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:[NSBundle mainBundle]];
ivc.FirstName = FirstName;
ivc.LastName=LastName;
ivc.Title开发者_开发技巧=[dictionary objectForKey:@"Details of Person"];
[self.navigationController pushViewController:ivc animated:YES];
[ivc release];
}
Please help me out...
Thanks
Check that you are retrieving the value from copyListOfItems in -didSelectRowAtIndexPath.
EDIT:
You should clearly understand the logic first. SearchedTableDatasource contains the tableViews complete set of data to be loaded in the tableview. copyListofItems contains filtered items. So in didSelectRowAtIndexPath you should code as follows,
if(searching==YES)
{
//retrieve the values from copyListofItems array
}
else
{
//retrieve the values from SearchedTableDatasource array
}
And also you are using a void function for searching, if so declare the copyListofItems array as global, or else make the function to return a NSMutableArray instance.
Change your init method in didSelectrowatIndexPath method and pass the dictionary object and set the DetailViewController attributes there, i hope that will work as it worked for me.
精彩评论