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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:06:51+00:00 2026-05-28T01:06:51+00:00

I know how to work with UITableview delegates and datasource protocol methods. So, I

  • 0

I know how to work with UITableview delegates and datasource protocol methods. So, I can work with sections. Now I would like to use a single query to fetch all the records from the database, ie, select firstname, lastname from contacts order by firstname asc. (Now I am using 26 individual query, I know this is not the right solution)

Now I would like to group the contacts based on its alphabets for sections. If no contacts starts from letter S. Then the section for S should not appear. Can you please provide me the right code or any tutorial? Thanks

Can you plese remodify this script?

-(void)read_data_fromDB
{
objectsForCharacters = [[NSMutableDictionary alloc]init];

sqlite3 *db = [eikardAppDelegate getNewDBConnection];
arr_sectionTitles = [[NSMutableArray  alloc] init];
for(char c='A';c<='Z';c++)
{
    NSMutableString *query = nil;

    query = [NSMutableString stringWithFormat:@"select first_name, middle_name, last_name from phonebook  where first_name like '%c%%';",c];  


    const char *sql = [query UTF8String];
    sqlite3_stmt *selectAllStmt = nil;

    if(sqlite3_prepare_v2(db,sql, -1, &selectAllStmt, NULL)!= SQLITE_OK)
        NSAssert1(0,@"error preparing statement",sqlite3_errmsg(db));
    else
    {
        NSMutableArray *arr_persons = [[NSMutableArray alloc] init];
        while(sqlite3_step(selectAllStmt)==SQLITE_ROW)
        {
            //NSLog(@"Firstname : %@",query);
            PersonInfo *person =[[PersonInfo alloc] init];

            char *chrstr =(char *)sqlite3_column_text(selectAllStmt, 0);
            if(chrstr !=NULL)
            {
                person.str_firstName = [NSString stringWithUTF8String:chrstr];
                NSLog(@"Firstname : %@",person.str_firstName);
            }       
            chrstr =(char *)sqlite3_column_text(selectAllStmt, 1);
            if(chrstr !=NULL)
            {

                person.str_middleName = [NSString stringWithUTF8String:chrstr];
                NSLog(@"Middlename : %@",[NSString stringWithUTF8String:chrstr]);
            }
            chrstr =(char *)sqlite3_column_text(selectAllStmt, 2);
            if(chrstr !=NULL)
            {
                person.str_lastName = [NSString stringWithUTF8String:chrstr];
                NSLog(@"Lastname : %@",person.str_lastName);
            }               


            [arr_persons addObject:person];

            [person release];
        }
        if([arr_persons count]>0)
        {
            NSString *keyValue = [NSString stringWithFormat:@"%c",c];
            [objectsForCharacters setObject:arr_persons forKey:keyValue];
            [arr_sectionTitles addObject:keyValue];

        }
        //[arr_persons release];

    }
    sqlite3_finalize(selectAllStmt);
}
sqlite3_close(db); }

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{

    return [arr_sectionTitles count];
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *secTitle = [arr_sectionTitles objectAtIndex:section];

    return [[objectsForCharacters objectForKey:secTitle] count];
}
  • 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-05-28T01:06:51+00:00Added an answer on May 28, 2026 at 1:06 am

    It depends of the origin of the data. If you are using a NSFetchedResultsController then you can use initWithFetcRequest:managedObjectContext:sectionNameKeyPath:cacheName: with the key name of the property for the sectionNameKeyPath:. You will multi-section ordered results.

    If you have the result data from the query (for the whole selection, not for just one of the 26 chars) in an array, then you’d better rearranging data in an ordered array of arrays. That is, each element of the master array is the array of results for each letter in index.

    if you use [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles] as titles for sections, it is easy to implement an index for the table. You don’t have to create a section for each index, you will reference the correct section for each index in the method tableView:sectionForSectionIndexTitle:atIndex:

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

Sidebar

Related Questions

can i know how to work with sitemap path in asp.net is possible examle
I know downcasting like this won't work. I need a method that WILL work.
I can subclass the cell and resolve the problem, but I'd like to know
I'd like to know if I should expect setFetchOffset to work in an NSFetchedResultsController.
I know there is ongoing work for in this regards but what is the
I know this won't work. I tried it in various forms and failed all
I know what MVC is and I work in webforms but I don't know
I know that XSLT does not work in procedural terms, but unfortunately I have
I know how to make the intellisense work in a .js file using this
Anyone know how to get this to work if it's possible? class Foo def

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.