gpt4 book ai didi

iphone - UITableview,其中包含按排序顺序排列的名称部分

转载 作者:行者123 更新时间:2023-11-29 13:42:09 25 4
gpt4 key购买 nike

我知道如何使用 UITableview 委托(delegate)和数据源协议(protocol)方法。所以,我可以使用部分。现在我想使用一个查询从数据库中获取所有记录,即从按名字升序排列的联系人中选择名字、姓氏。 (现在我正在使用 26 个单独的查询,我知道这不是正确的解决方案)

现在我想根据部分的字母表对联系人进行分组。如果没有联系人以字母 S 开头。则不应出现 S 部分。你能给我提供正确的代码或任何教程吗?谢谢

你能重新修改这个脚本吗?

-(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];
}

最佳答案

这取决于数据的来源。如果您使用的是 NSFetchedResultsController,那么您可以将 initWithFetcRequest:managedObjectContext:sectionNameKeyPath:cacheName: 与 sectionNameKeyPath: 属性的键名一起使用。您将得到多部分有序结果。

如果您将查询的结果数据(针对整个选择,而不仅仅是 26 个字符中的一个)放在一个数组中,那么您最好将数据重新排列在一个有序数组中。也就是说,主数组的每个元素都是索引中每个字母的结果数组。

如果你使用[[UILocalizedIndexedCollat​​ion currentCollat​​ion] sectionIndexTitles]作为sections的标题,很容易为表格实现索引。您不必为每个索引创建一个部分,您将在方法 tableView:sectionForSectionIndexTitle:atIndex: 中为每个索引引用正确的部分:

关于iphone - UITableview,其中包含按排序顺序排列的名称部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8787681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com