Reputation: 837
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];
}
Upvotes: 1
Views: 537
Reputation: 3359
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:
Upvotes: 1