randomor
randomor

Reputation: 5673

Getting the self.tableview indexpath from UISearchDisplayController tableView indexpath

So I have a detail view that need to display information by getting a index integer caculated from the indexpath from the selected cell in self.tableview, I've been using the NSFetchedResultsController for the self.tableview too. I've also implemented a UISearchDisplayController to do search.

Question: How do I convert the selected indexPath in the UISearchDisplayController tableview to the indexpath of the original self.tableview? Or do I need to set up a NSArray instance and loop through it to find out the index? What's the most efficient way to do it?

Here is the code:

-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
    NSLog(@"display project details");
    if (tableView == self.table) {
        [parentController projectSelectedFromList:indexPath.row];
    }else{
        NSInteger index;
        //what to put here? To get the indexPath of the self.table from search tableview 
        [parentController projectSelectedFromList:index];
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

Upvotes: 0

Views: 1891

Answers (4)

Codetard
Codetard

Reputation: 2605

Swift version of @Deepak Danduprolu answer:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    var selectedObject = searchedArray[indexPath.row]
    var index = OrignalArray.index(of: selectedObject)
    print("index", index) // This will give you index of selected array from the original array
}

Upvotes: 0

randomor
randomor

Reputation: 5673

I found out the solution:

-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
NSLog(@"display project details");
if (tableView == self.table) {
    [parentController projectSelectedFromList:indexPath.row];
    NSLog(@"indexpath at orignal tableview is: %@", [indexPath description]);
}else{
    NSIndexPath *indexPathForOriginal = [resultsController indexPathForObject: [self.filteredResults objectAtIndex:indexPath.row]];
    NSInteger index = indexPathForOriginal.row;
    [parentController projectSelectedFromList:index];
    NSLog(@"indexpath at search tableview is: %@", [indexPathForOriginal description]);

}
[tableView deselectRowAtIndexPath:indexPath animated:YES];

}

Upvotes: 0

Deepak Danduprolu
Deepak Danduprolu

Reputation: 44633

Assuming no duplicates,

id selectedObject = [searchArray objectAtIndex:indexPath.row];
index = [sourceArray indexOfObject:selectedObject];

Idea is pretty simple. Get the selected object of your search array as it will map directly to the index path's row. Then search for the object's index within the source or master array. This should give you the index you want.

Upvotes: 3

Obliquely
Obliquely

Reputation: 7072

Look at what you have you got in tableView:cellForRowAtIndexPath: of your tableView data source (usually the tableViewController object). Assuming you do a similar check on whether you are using the standard tableView or the search tableView there - and if you're not, then you're search filter surely won't be effective - you just need to have analogous code in the your tableView:didSelectRowAtIndexPath:.

If I'm missing the point here then apologies... and you might need to say a bit more in your question.

Upvotes: 0

Related Questions