Search code examples
iphoneuitableviewuisearchdisplaycontrollerdidselectrowatindexpath

How to get selected row from UITableView with Search Display Controller


I have my search display view controller based off TableSearch.

Normally, following expression gives selected index row:

self.tableView.indexPathForSelectedRow.row

However while in search mode, selected row can be obtained using:

self.searchDisplayController.searchResultsTableView.indexPathForSelectedRow.row

I use the above indices to remove respective rows (from my back end arrays) soon after some processing - at the end of didSelectRowAtIndexPath. The arrays I use are listContent and filteredListContent - as per the Apple example.

My issue is, while in search mode, I remove a row using:

[self.filteredListContent removeObjectAtIndex: self.searchDisplayController.searchResultsTableView.indexPathForSelectedRow.row]

However, at the same time, I also want to remove the same object from self.listContent because when I return to non-search mode, that row should not appear.

I saw that self.tableView.indexPathForSelectedRow.row does not update regularly while in search results view. Instead, it gives the last selected row before I entered the search results view.

Off course, I can put some content into my array objects so that both indexes can be cross-referenced. But is there any efficient solution other than that? I think table view should have this mechanism.

And yes, I already do following in my viewWillAppear:

   [self.tableView deselectRowAtIndexPath:self.tableView.indexPathForSelectedRow animated:YES];

Solution

  • This is working fine for me...

     #pragma mark - delete row
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (editingStyle == UITableViewCellEditingStyleDelete)
        {
    
            if(tableView == self.searchDisplayController.searchResultsTableView)
                 // searching mode
            {
                // find row (index) from listContent
                arrayObject = [filteredlistContent objectAtIndex:indexPath.row];
                NSUInteger fooIndex = [listContent indexOfObject: arrayObject];
                NSLog(@"index listContent %u", fooIndex);
                //remove from listContent
                [self.listContent removeObjectAtIndex:fooIndex];
                [self.tableView reloadData];
    
                //remove from filteredlistContent
                [self.filteredlistContent removeObjectAtIndex:indexPath.row];
                [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    
            }
            else  //  tableView mode
            {
            [self.listContent removeObjectAtIndex:indexPath.row];
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            }
        }
    }