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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:57:20+00:00 2026-06-01T12:57:20+00:00

I followed a tutorial I found online to create a tableview with sections and

  • 0

I followed a tutorial I found online to create a tableview with sections and an index from an array of custom objects. This code works with the exception that when I select a row in the table I the index path for that section and not for the entire array. I can see why it doesn’t work but I can’t figure out how to address the fix, this is my cell for tableview code.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"NameCell";


UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}



// Configure the cell...


    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    int displayOrder = [defaults integerForKey:@"displayOrder"];
    int sortOrder = [defaults integerForKey:@"sortOrder"];

    NSString *alphabet = [listIndex objectAtIndex:[indexPath section]];

    NSPredicate *sectionPredicate = [[NSPredicate alloc] init];

    if (sortOrder == 1) {  
        //NSLog(@"fName is predicate at cell level");
        sectionPredicate = [NSPredicate predicateWithFormat:@"fName beginswith[c] %@", alphabet];
    } else {
        //NSLog(@"lName is predicate at cell level");
        sectionPredicate = [NSPredicate predicateWithFormat:@"lName beginswith[c] %@", alphabet];
    }
    NSArray *sectionContacts = [filteredList filteredArrayUsingPredicate:sectionPredicate];

if (isSearching) {
    current = [filteredList objectAtIndex:indexPath.row];
} else{
    current = [sectionContacts objectAtIndex:indexPath.row];        
}

if (displayOrder == 1) {
    NSString *fullName = [NSString stringWithFormat:@"%@ %@",[current valueForKey:@"fName"],[current valueForKey:@"lName"]];
    [cell.textLabel setText:fullName];  
    //NSLog(@"FirstNameFirst");

} else {
    NSString *fullName = [NSString stringWithFormat:@"%@ %@",[current valueForKey:@"lName"],[current valueForKey:@"fName"]];
    [cell.textLabel setText:fullName];
    //NSLog(@"LastNameFirst");

}

    [cell.detailTextLabel setText:[current valueForKey:@"extension"]];

return cell;  }

THen I call the segue with this code.

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

if ([segue.identifier isEqualToString:@"showContact"]) {

    DetailViewController *dvc = [segue destinationViewController];
    NSIndexPath *path = [self.tableView indexPathForSelectedRow];
    NSDictionary *c = [filteredList objectAtIndex:path.row];
    [dvc setCurrentContact:c];   
    [searchBar resignFirstResponder];
} }

The problem is that the objectAtIndex:path.row returns the index for that section but it isn’t modified for the entire array, so if a name in the “B” section that is at index 4 of that section is tapped it returns the object at index 4 of the primary array. I have been scratching my head to figure out how to get the index for the full array and not for the one that is only local to that section.

I’ll buy you a 6 pack of your favorite beverage if you can help!

Thanks!

  • 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-01T12:57:21+00:00Added an answer on June 1, 2026 at 12:57 pm

    You do it the same way that they do it in the first function, so change your prepareForSegue to this:

    -(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        if ([segue.identifier isEqualToString:@"showContact"]) {
            DetailViewController *dvc = [segue destinationViewController];
            NSIndexPath *path = [self.tableView indexPathForSelectedRow];
    
            NSDictionary *c;
            NSString *alphabet = [listIndex objectAtIndex:[path section]];
            NSPredicate *sectionPredicate = [[NSPredicate alloc] init];
            if (sortOrder == 1) {  
                sectionPredicate = [NSPredicate predicateWithFormat:@"fName beginswith[c] %@", alphabet];
            } else {
                sectionPredicate = [NSPredicate predicateWithFormat:@"lName beginswith[c] %@", alphabet];
            }
            NSArray *sectionContacts = [filteredList filteredArrayUsingPredicate:sectionPredicate];
    
            if (isSearching) {
                c = [filteredList objectAtIndex:path.row];
            } else{
                c = [sectionContacts objectAtIndex:path.row];        
            }
    
            [dvc setCurrentContact:c];   
            [searchBar resignFirstResponder];
        } 
    }
    

    Note that it would probably be best to pull the common code out and make a separate function instead of using it twice like this.

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

Sidebar

Related Questions

I followed this tutorial and the source code : http://blog.objectgraph.com/index.php/2010/04/20/encrypting-decrypting-base64-encode-decode-in-iphone-objective-c/ And then, I downloaded
I have found this tutorial online http://net.tutsplus.com/tutorials/php/creating-a-php5-framework-part-1/ I have created myself a simple sort
I have followed this tutorial to create the first azure application http://msdn.microsoft.com/en-us/WAZPlatformTrainingCourse_IntroToWindowsAzureLabVS2010 Because after
I have a JSF/RichFaces setup, and I found this tutorial . I followed it
I followed the tutorial found here and added my own flare to it: tutorial
I followed this tutorial on configuring the Rails plugin ExceptionNotifier. I know that I
I followed the Tutorial in Cocoa Programming For Mac OS X to create a
I followed this tutorial: https://github.com/EllisLab/CodeIgniter/wiki/PDF-generation-using-dompdf However, I can't seem to set the path right.
I followed this tutorial here: http://www.dannyherran.com/2011/02/facebook-php-sdk-and-codeigniter-for-basic-user-authentication/ It works great but when I enable CSRF
I followed this tutorial http://www.tugberkugurlu.com/archive/api-key-authorization-through-query-string-in-asp-net-web-api-authorizationfilterattribute to create custom Authorization filter. I have CarController with

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.