iPhone app returns array item 0 during search every time (confused on indexPath.row)

dalearyous

Senior member
Jan 8, 2006
836
0
0
i have a table with search bar that when you begin to type it narrows down your selections below the search bar. basically the first array let say has 4 names: (0)bob, (1)jim, (2)david, & (3)carl. when you select anyone one it loads their correct detailview based on the index (0,1,2,3). however, when you go to search and type d it narrows the table down to just (0)david which is now in row 0. so when you select david it loads bob from the original array.

someone told me to either retain the original value of indexPath.row (prior to the search), or pass it to a new index variable ... no idea how to do that lol

i can post code snippets or send project. so frustrating!
 

Patterner

Senior member
Dec 20, 2010
227
0
0
Code would help, but I've worked on some tableviews lately so I'll take a quick stab at it.

Sounds like you should be passing in a copy of the array and then returning the object based on that, rather than just changing the tableview, that way when you call didSelectObjectAtIndexPath you can send [indexpath row] to the copy of the array to get the correct item.

Otherwise, you could use an NSDictionary instead of an array and call for the object based on the key of the item selected.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Find another way to maintain the mapping between the row and the index in the array. Using the index into the tableView collection obviously won't work in this context. I've been off iOS stuff for a couple months now, but I think you can associate some user data with a row in a UITableView. That might be a good approach.
 

speg

Diamond Member
Apr 30, 2000
3,681
3
76
www.speg.com
I just started with iOS so I'm not of much help. I just loaded my first file into a table today so my next step will be something very similar to what you're doing. Can you put something in the cell to use as the index?
 

dalearyous

Senior member
Jan 8, 2006
836
0
0
here are the two methods that i think are associated with the issue:

Code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIViewController *detailsViewController = [[UIViewController alloc] init];
	/*
	 If the requesting table view is the search display controller's table view, configure the next view controller using the filtered content, otherwise use the main list.
	 */
	Physician *physician = nil;
	if (tableView == self.searchDisplayController.searchResultsTableView)
	{
        physician = [self.filteredListContent objectAtIndex:indexPath.row];
    }
	else
	{
        physician = [self.listContent objectAtIndex:indexPath.row];
    }
	detailsViewController.title = physician.name;
	
	NSString* physicianName = [tmpImages objectAtIndex:indexPath.row];
	UIImage* physicianImage = [UIImage imageNamed:physicianName];
	UIImageView* physicianImageView = [[UIImageView alloc] initWithImage:physicianImage];
	physicianImageView.frame = self.view.bounds;
	[detailsViewController.view addSubview:physicianImageView];
	[physicianImageView release];
	
	
    [[self navigationController] pushViewController:detailsViewController animated:YES];
    [detailsViewController release];
}


#pragma mark -
#pragma mark Content Filtering

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
	/*
	 Update the filtered array based on the search text and scope.
	 */
	
	[self.filteredListContent removeAllObjects]; // First clear the filtered array.
	
	/*
	 Search the main list for Physicians whose type matches the scope (if selected) and whose name matches searchText; add items that match to the filtered array.
	 */
	for (Physician *physician in listContent)
	{
		if ([scope isEqualToString:@"All"] || [physician.type isEqualToString:scope])
		{
			NSComparisonResult result = [physician.name compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
            if (result == NSOrderedSame)
			{
				//[self.filteredListContent addObject:physician];
				[self.filteredListContent addObject:physician];
            }
		}
	}
}
 

Patterner

Senior member
Dec 20, 2010
227
0
0
here are the two methods that i think are associated with the issue:

Hmmm, couple of thoughts based on the code. Is this:
Code:
if (tableView == self.searchDisplayController.searchResultsTableView)
ever evaluating to true?

Also, I've had an issue with arrays in the past that had to do with them needing a separate initialization even if they were in intialised in the init() function for the object. After filterContentForSearchText() runs make sure that this line:
Code:
[self.filteredListContent addObject:physician];
actually added the objects to the array like you're thinking it should.

Another thing I would consider is assigning each physician object a unique number and storing them in a NSMutableDictionary with that number as the key instead of an array. You can still use the AllObjects method (or an ObjectEnumerator) of the dictionary if you need to iterate through them, but recalling them from the Dictionary using a key value would probably be easier once you've created the filtered list, which seems to be working fine.