Iphone 如何在从数据库获取内容的UITableView中实现字母顺序的节标题
到目前为止,我已经通过填充数据库中的内容实现了Iphone 如何在从数据库获取内容的UITableView中实现字母顺序的节标题,iphone,objective-c,sqlite,uitableview,Iphone,Objective C,Sqlite,Uitableview,到目前为止,我已经通过填充数据库中的内容实现了UITableView 通过在数组中从sqlite数据库中检索 storedContactsArray=[Sqlitefile selectAllContactsFromDB] 因此,没有多个节、节标题和返回storedContactsArray.count作为行数 现在,我需要在表视图中填充相同的数据,但数据集按字母顺序排列在表的各个部分。 我试过了 alphabetsArray =[[NSMutableArray alloc]initWithOb
UITableView
通过在数组中从sqlite数据库中检索
storedContactsArray=[Sqlitefile selectAllContactsFromDB]代码>
因此,没有多个节、节标题和返回storedContactsArray.count
作为行数
现在,我需要在表视图中填充相同的数据,但数据集按字母顺序排列在表的各个部分。
我试过了
alphabetsArray =[[NSMutableArray alloc]initWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",nil];
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [alphabetsArray count];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return alphabetsArray;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [alphabetsArray objectAtIndex:section];
}
但如果numberOfRowsInSectio
n,则会失败,因为storedContactsArray
中最初没有联系人
出现错误:-[\uu NSArrayM objectAtIndex:]:索引25超出空数组的界限
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[storedContactsArray objectAtIndex:section] count]
}
任何关于使用完整链接的建议,请为了达到您的要求,您需要首先按照字母顺序将所有数据分离出来。具体如下
这里的部分是一个可变字典,我们将在其中以字母集的形式获取所有数据
//Inside ViewDidLoad Method
sections = [[NSMutableDictionary alloc] init]; ///Global Object
BOOL found;
for (NSString *temp in arrayYourData)
{
NSString *c = [temp substringToIndex:1];
found = NO;
for (NSString *str in [sections allKeys])
{
if ([str isEqualToString:c])
{
found = YES;
}
}
if (!found)
{
[sections setValue:[[NSMutableArray alloc] init] forKey:c];
}
}
for (NSString *temp in arrayYourData)
{
[[sections objectForKey:[temp substringToIndex:1]] addObject:temp];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[sections allKeys]count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[sections valueForKey:[[[sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:section]] count];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString* CellIdentifier = @"Cell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == Nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSString *titleText = [[sections valueForKey:[[[sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
cell.textLabel.text = titleText;
return cell;
}
请尝试一下,我正在使用它,它工作得很好
希望它能帮助你 谢谢,visable的效果很好,但除了我们的阵列之外,其他内容似乎不符合顺序。Evey time details textlabel显示每个节“Contact*Contact=[storedContactsArray objectAtIndex:indexPath.row]中第一节中的内容;cell.detailTextLabel.text=contact.companyName';我编辑了我的问题,你能让它工作吗?我正在做类似的事情,即使有下面的建议,但我的数据并没有按字母顺序显示。它被正确地分组,但不是按字母顺序。