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 8265407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:47:27+00:00 2026-06-08T04:47:27+00:00

I have a UITableView populated by a SQLite database. I added Section-based Grouping using

  • 0

I have a UITableView populated by a SQLite database. I added Section-based Grouping using the sectionIndexTitlesForTableView delegate method today and now when a Cell is selected, the String for indexPath.row is not the same as the text in the selected Cell.

My Code works like this.

  1. I create an Array that holds the businesses from the SQLite database.
  2. I sort that Array alphabetically.
  3. I create an Array of letters of the Alphabet using only the letters of the Alphabet that businesses in the database begin with.
  4. I use that Array, along with an NSPredicate to provide Grouped Header views which group the businesses by their first letter, alphabetically.
  5. The Selected Row is written to the NSUserDefaults file, and a View Controller is pushed (iPhone), or an Observer is added for that key (iPad).

Unfortunately, since adding the header views, indexPath.row now returns a completely different string to that of the TextLabel of the selected Cell, and so a different Business’ information is displayed.

Here are the important blocks of code for the main arrays.

- (void)viewDidLoad
{
 // Lots of code...
 arrayName = [[NSMutableArray alloc] init];
 NameSet = [[NSMutableSet alloc] init];
 sortedArray = [arrayName sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

 alphabet = [[NSMutableArray alloc] init];
 [alphabet addObject:@"{search}"];
 for (int i=0; i<[sortedArray count]-1; i++)
 {
  char alphabetUni = [[sortedArray objectAtIndex:i] characterAtIndex:0];
  NSString *uniChar = [NSString stringwithFormat:@"%c", alphabetUni];
  if (![alphabet containsObject:uniChar])
  {
    [alphabet addObject:uniChar];
  }
 }
 }

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 {
  return [alphabet count];
 }

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
 NSInteger rows = 0;

 NSString *alpha = [alphabet objectAtIndex:section];

 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alpha];
 businesses = [sortedArray filteredArrayUsingPredicate:predicate];

 if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]){
    rows = [self.searchResults count];
 }
 else {
    rows = [businesses count];
 }

  return rows;
 }

 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection (NSInteger)section
 {
  return [alphabet objectAtIndex:section];
 }

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
  NSString *CellIdentifier = @"Cell";
  cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil) {
    cell = [[UITableViewCell alloc]
             initWithStyle:UITableViewCellStyleDefault
             reuseIdentifier:CellIdentifier];

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  }

  NSString *alpha = [alphabet objectAtIndex:indexPath.section];
  NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alpha];
  businesses = [sortedArray filteredArrayUsingPredicate:predicate];

  if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]){
    cell.textLabel.text =
    [self.searchResults objectAtIndex:indexPath.row];
  }
  else{
        NSString *cellValue = [businesses objectAtIndex:indexPath.row];
        cell.textLabel.text = cellValue;
  }
  return cell;

  }

  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
 NSString *selected = nil;

 if (tableView == self.tableView)
 {
    selected = [businesses objectAtIndex:indexPath.row];
 }
 else if (tableView == searchDis.searchResultsTableView)
 {
    selected = [filteredData objectAtIndex:indexPath.row];
 }

 [def setObject:selected forKey:@"NameChoiceDetail"];

 if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
 {
    [self performSegueWithIdentifier:@"NameDetailPush" sender:self];

 }
 }

// Please excuse my horribly written code. I’ve only been working with Objective-C for 4 months, and Programming for about 8 months. Any suggestions/optimisations will be duly noted.

  • 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-08T04:47:29+00:00Added an answer on June 8, 2026 at 4:47 am

    Your table view uses sections but your implementation of tableView:didSelectRowAtIndexPath: doesn’t evaluate the section of the index path. So the code is missing something.

    Furthermore, I find that your use of the businesses variable (it’s probably an instance variable) very strange. It is assigned a value in tableView:cellForRowAtIndexPath: but not in tableView:didSelectRowAtIndexPath: even though it is used there. So the outcome if the latter depends on what table cell was displayed last and as a consequence it depends on scrolling user interaction. That might be the reason why the outcome looks rather random.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a UITableView that's populated using core data & sqlite. I'd like to
I have a UITableView populated with a location-based datasource. I'm calling [self updateView]; to
I have a UITableView that is being populated by core data. Now, when I
I have a uitableview that is populated from a sqlite query. I want to
I have a UITableView populated by a feed from searchtwitter.com. I also have a
I have a working UITableView in my view controller. It is being successfully populated
I have implemented the search method for a UITableView populate from a NSArray (mylist):
I have a UITableView populated (previously saved via a button in navbar), with transactions.
I have an UITableView populated by a NSFetchedResultsController . The initial fetch works fine.
I have a UITableView that gets populated from CoreData via a controller that implements

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.