我正在使用此查询按ASC顺序获取数据 从list_tbl中选择*其中cat_id =%d按名称排序ASC
现在我的表视图显示了基于ASC顺序的数据 然后我用它来获取右侧A到Z的字母
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)help
{
return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] objectAtIndex:help];
}
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];
}
现在我的问题是如何基于章节标题划分数据,就像所有A内容转到A然后B转到Z,当用户点击右侧的任何字母时,它应该转到该部分
//更新///////////////
-(void)setUpRecordsForIndexing
{
for (NSString * recordName in appDelegate.catLists)//where app.catList contains all the value
{
NSString *firstLetter = ([recordName length] ==0)?miscKey:[[recordName substringToIndex:1]uppercaseString];
/*if(![firstLetter beginsWithCharacterInSet:[NSCharacterSet letterCharacterSet]] || [recordName isEqualToString:@""] || recordName == nil )
firstLetter = miscKey ;
*/
NSMutableArray *indexArray = [IndexedRecords objectForKey:firstLetter];
if (indexArray == nil)
{
indexArray = [[NSMutableArray alloc] init];
[IndexedRecords setObject:indexArray forKey:firstLetter];
[IndexLetters addObject:firstLetter];
[indexArray release];
}
[indexArray addObject:record];// this is not workking
// NSLog(@" index array is %@",indexArray);
}
}
我的INDEXRECORD没有显示正确的值 我现在可以基于ABCD划分该部分,但每个部分都有相同的内容,我做错了
答案 0 :(得分:2)
例如:说RecordsArray包含你的所有记录,现在在表视图中数据源方法numberOfSectionsInTableView
再调用一个方法来索引你所有的记录,如下所示
-(void)setUpRecordsForIndexing
{
if(IndexedRecords)
{
[IndexedRecords release];
IndexedRecords = nil;
}
if(IndexLetters)
{
[IndexLetters release];
IndexLetters = nil;
}
IndexedRecords = [[NSMutableDictionary alloc] init];
IndexLetters = [[NSMutableArray alloc] init];
NSString *miscKey = @"#";
for (NSString * recordName in RecordsArray)
{
NSString *firstLetter = ([recordName length] ==0)?miscKey:[[recordName substringToIndex:1]uppercaseString];
if(![firstLetter beginsWithCharacterInSet:[NSCharacterSet letterCharacterSet]] || [recordName isEqualToString:@""] || recordName == nil )
firstLetter = miscKey ;
NSMutableArray *indexArray = [mIndexedRecords objectForKey:firstLetter];
if (indexArray == nil)
{
indexArray = [[NSMutableArray alloc] init];
[IndexedRecords setObject:indexArray forKey:firstLetter];
[IndexLetters addObject:firstLetter];
[indexArray release];
}
[indexArray addObject:record];
}
}
并在numberOfSectionsInTableView
中返回IndexLetters
的计数。我希望这能解决你的问题。
答案 1 :(得分:1)
在索引记录时,请将您的记录保存在字典中,如IndexedRecords。这样,对于键“A”设置,记录数组以字母“A”开头。并填充表格视图
[[IndexedRecords objectForKey:[IndexLetters objectAtIndex:indexPath.section]]objectAtIndex:indexPath.row]
IndexLetters也是一个包含当前列表的索引字母的数组。