thanks in advance.
I'm trying to create an app that has arrays with names, pictures and numbers. I already have the table view created with all the data in the respective arrays; I already have the Search bar working and displaying the correct search. What I'm missing is showing the results of the search bar in another view controller. (I already got this working with the Table View: I select a name and it opens another view and then show me the picture and the number etc,) I'm just missing the same thing but when I do the search.
Here is my code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = @"DreamCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if (tableView == self.tableView) {
cell.textLabel.text = [Names objectAtIndex:indexPath.row];
cell.imageView.image =[UIImage imageNamed:[Images objectAtIndex:indexPath.row]];
} else{
cell.textLabel.text = [searchResults objectAtIndex:indexPath.row];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (self.searchDisplayController.isActive) {
[self performSegueWithIdentifier:@"ShowDreamsDetails" sender:self];
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
if ([segue.identifier isEqualToString:@"ShowDreamsDetails"]) {
NSString *object =nil;
// NSIndexPath *indexPath =nil;
if (self.searchDisplayController.isActive) {
indexPath =[[self.searchDisplayController searchResultsTableView] indexPathForSelectedRow];
object = [searchResults objectAtIndex:indexPath.row];
/* *Here is where I have the problem, If I leave it like thi,s it only shows the first row in my array; I needed to send the correct name and details to the other View Controller. */
DesciptionViewController *desViewController = segue.destinationViewController;
desViewController.PrincipalName = [Names objectAtIndex:indexPath.row ];
desViewController.PrincipalDescription = [Description objectAtIndex:indexPath.row];
desViewController.PrincipalNumber = [Numeros objectAtIndex:indexPath.row];
desViewController.Images1 = [UIImage imageNamed: [Images objectAtIndex: indexPath.row]];
} else {
DesciptionViewController *desViewController = segue.destinationViewController;
desViewController.PrincipalName = [Names objectAtIndex:indexPath.row];
desViewController.PrincipalDescription = [Description objectAtIndex:indexPath.row];
desViewController.PrincipalNumber = [Numeros objectAtIndex:indexPath.row];
desViewController.Images1 = [UIImage imageNamed: [Images objectAtIndex: indexPath.row]];
}
}
}
Thanks in advance to anyone, and I'm sorry if this is a duplicate question; I tried to search around, but I did not find anything.
It's not clear from this code as to what's being stored in your searchResults array. Normally this would be objects representing a Principal, which would have properties of Name, Description, Numeros, and Image, but you're clearly keeping those in separate arrays.
Somehow when you do a search and display the results, you're accessing those arrays; you need to recreate here what you did there. In other words, you know you have search result #row, now you need to map that (I guess through searchResults) to the correct row in your original arrays.
In other words, please post the code for tableView:cellForRowAtIndexPath
EDIT: after code for cellForRowAtIndexPath: added
Ok, so you've only got the Names in your searchResults, which means that you'll have to find the other data.
Again, it would be easiest and best to create a new object type Principal, which has properties of Name, Description, Numeros, and Image, then your main array and your searchResults would not be strings, but Principals, and you could easily handle giving the appropriate information to your viewcontroller. (In fact, your sub view controller might have a property Principal *person, and you could just assign it the appropriate Principal from either your main array or your search array.
But...if you want to patch this and make it work, you >could< search in the Names array for the name of the selected Name from searchResult. (This assumes that no two people have the same Name, or else it will always find the first one.)
So your prepareForSegue changes to:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
if ([segue.identifier isEqualToString:@"ShowDreamsDetails"]) {
NSInteger realRow = indexPath.row;
if (self.searchDisplayController.isActive) {
indexPath =[[self.searchDisplayController searchResultsTableView] indexPathForSelectedRow];
NSString * name = (NSString *) [searchResults objectAtIndex:indexPath.row];
realRow = [Names indexOfObject:name];
}
if (realRow != NSNotFound) {
DesciptionViewController *desViewController = segue.destinationViewController;
desViewController.PrincipalName = [Name objectAtIndex: row];
desViewController.PrincipalDescription = [Description objectAtIndex: row];
desViewController.PrincipalNumber = [Numeros objectAtIndex: row];
desViewController.Images1 = [UIImage imageNamed: [Images objectAtIndex: row]];
} else {
NSLog(@"Well, this is very odd; an invalid indexPath or a name in searchResults that's not in Names");
}
}
}
But again, it would be MUCH better to define a Principals object and have that in your two arrays.