Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8281757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:08:40+00:00 2026-06-08T10:08:40+00:00

I have a table view set up with a search bar. Everything works great:

  • 0

I have a table view set up with a search bar. Everything works great: when I type text into the search bar, the results update in the table view based on my search text. The problem I’m having is in determining what the ID of the table cell is to pass that along when I’m going to my detail view. To clarify: the cells in the table view are usually generated from a SQLite3 database and ordered by their name. So if I have 4 cells: (Cabernet Cortis, Cabernet Franc, Cabernet Sauvignon and Ruby Cabernet), their IDs are normally (78, 79, 80, 378). However when I search “cabernet”, and these four items come up, the indexPath.row is (1, 2, 3, 4) instead of (78, 79, 80, 378) so it’s taking me to the wrong grape in the detail view (which are the first four grapes in alpha order as entered into my DB).

Here is some code to help clear things up. I think what would help is if I can obtain the ID of each grape that comes up as a search result and also have an MSMutableArray of those values, and then use [actualIDNumber objectAtIndex:indexPath.row] instead of just indexPath.row.

//ViewGrapesViewController.h
@property (strong, nonatomic) IBOutlet UISearchBar *searchBar;
- (void) searchGrapesTableView;
@property (nonatomic, retain) NSMutableArray *listOfGrapes;
@property (nonatomic, retain) NSMutableArray *searchResult;
@property (nonatomic, retain) NSMutableArray *searchResultID;
@property (nonatomic) Boolean isSearchOn;

//ViewGrapesViewController.m
//synthesize variables, etc
- (void)viewDidLoad
    {
        //obtain grapes from the DB, etc.
        //add searchNames to an NSMutableArray
        listOfGrapes = [[NSMutableArray alloc] init];
        for (int i=1; i<=[self.grapes count]; i++) {
            Grapes *theGrape = [self.grapes objectAtIndex:(i-1)];
            NSString *grapeSearchName = theGrape.searchName;
            [listOfGrapes addObject:grapeSearchName];
        }
        searchResult = [[NSMutableArray alloc] init];
        searchResultID = [[NSMutableArray alloc] init];
        isSearchOn = NO;

    //search bar stuff
    searchBar.autocorrectionType = UITextAutocorrectionTypeNo;

    [super viewDidLoad];
}

- (void) searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    isSearchOn = YES;
}

- (void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    if ([searchText length] > 0) {
        isSearchOn = YES;
        [self searchGrapesTableView];
    }
    else {
        isSearchOn = NO;
    }
    [self.tableView reloadData];
}

- (void) searchGrapesTableView {
    [searchResult removeAllObjects];
    [searchResultID removeAllObjects];
    for (NSString *str in listOfGrapes)
    {
        NSRange titleResultsRange = [str rangeOfString:searchBar.text options:NSCaseInsensitiveSearch];
        if (titleResultsRange.length > 0) {
            [searchResult addObject:str];
            //would love to add ID to searchResultID array here, don't know how to do that
        }
    }
}

- (void) searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [self searchGrapesTableView];
}

- (void) searchBarTextDidEndEditing:(UISearchBar *)searchBar {
    isSearchOn = NO;
    [self.searchBar resignFirstResponder];
    [self.tableView reloadData];
}

//table view code
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //first check if the user is searching
    if (isSearchOn) {
        return 1;
    }
    //if not, then search by alpha order
    else {
        return 24;
    }
}

//create header titles & number of rows in section (it's a lot of code and works fine)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"grapeCell";
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    //is the user searching?
    if (isSearchOn) {
        NSString *cellValue = [searchResult objectAtIndex:indexPath.row];
        cell.textLabel.text = cellValue;
    }

    //otherwise...
    else {
        NSInteger theNumber;
        NSInteger numberToAdd = [[howManyRowsInSection objectAtIndex:indexPath.section] integerValue];
        theNumberToAdd = numberToAdd;
        theNumber = numberToAdd + indexPath.row;

        //cell title = grape name
        Grapes *grape = [self.grapes objectAtIndex:theNumber];
        NSString *theGrapeName = grape.grapeName;
        cell.textLabel.text = theGrapeName;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //is the user searching?
    if (isSearchOn) {
        theNumberToAdd=0; //which is wrong but here for the time being
        //i have to have the ID here in order to do some logic to figure out what row to return
    }

    //otherwise...
    else {
        theNumberToAdd = [[howManyRowsInSection objectAtIndex:indexPath.section] integerValue]; // i performed this logic elsewhere
    }


    ViewGrapeViewController *detailViewController = (ViewGrapeViewController *)self.presentedViewController;
    detailViewController.detailItem = [self.grapes objectAtIndex: (indexPath.row+theNumberToAdd)];
}

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"toGrapeDetail"]) {       
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        //is the user searching
        if (isSearchOn) {
            theNumberToAdd=0;  // which is wrong
        }

        //otherwise...
        else {
            theNumberToAdd = [[howManyRowsInSection objectAtIndex:indexPath.section] integerValue];
        }

        NSDate *object = [_objects objectAtIndex:(indexPath.row+theNumberToAdd)];
        [[segue destinationViewController] setDetailItem:object];
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T10:08:42+00:00Added an answer on June 8, 2026 at 10:08 am

    In your did select item method, get the text from the cell that was selected and iterate through your array until you find an item that matches, then open that cell’s detail view. This only will work if you have all unique item names.

    - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSString *str = [[[tableView cellForRowAtIndexPath:indexPath] textLabel] text];
        for (int j=0; j<whateverArray.count; j++)
        {
            NSString *temp = [whateverArray objectAtIndex:j];
            if ([str isEqualToString:temp])
            {
                //do whatever you would do if the user actually clicked the row at position j
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have set up a search bar for my table view, and I would
I have a view controller that has a tableView in it. When I set
I have table View with grouped style with three sections. Get information from dictionary
I have a table view of peoples names and when I click on one
I have a table view with numerous cells. When I press one, a tick
I have a table view controller with (among others) a cell that is to
I have a problem where the user has set his preferences in a table.
I'm working on a view-based iPhone app that has the following flow: search ->
I have table view which is populated from an array grabbed from a url.
I am trying to add a UISearchbar to table header view. I have the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.