在iphone上的UItableview中对数据进行排序和显示

在iphone上的UItableview中对数据进行排序和显示,iphone,Iphone,我使用此查询按ASC订单获取数据 从列表中选择*,其中类别id=%d按名称排序ASC 现在,我的表视图显示基于ASC顺序的数据 然后我用这个来得到右边从A到Z的字母 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)help { return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] o

我使用此查询按ASC订单获取数据 从列表中选择*,其中类别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将部分划分,但每个部分都有相同的内容,我做错了什么

在为记录编制索引时,请将记录保存在词典中,比如IndexedRecords。这样,对于键“A”,设置记录数组以字母“A”开头。在填充表视图时

[[IndexedRecords objectForKey:[IndexLetters objectAtIndex:indexPath.section]]objectAtIndex:indexPath.row]

IndexLetters也是一个数组,包含当前列表的索引字母。

为记录编制索引时,请将记录保存在字典中,例如IndexedRecords。这样,对于键“A”,设置记录数组以字母“A”开头。在填充表视图时

[[IndexedRecords objectForKey:[IndexLetters objectAtIndex:indexPath.section]]objectAtIndex:indexPath.row]

IndexLetters也是一个数组,包含当前列表的索引字母。

例如:假设records数组包含所有记录,现在在表视图数据源方法中,调用另一个方法为所有记录编制索引,如下所示

-(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
中返回
索引器的计数。我希望这能解决您的问题。

例如:假设RecordsArray包含您的所有记录,现在在表视图datasource method
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
中返回
索引器的计数。我希望这能解决您的问题。

好吧,假设我的所有数据都在appDelegate.ad中,其中ad是NSMutableArry那么???@prajakta,是的,在重新加载表中的数据之前,您可以访问appDelegate NSMutableArry变量。如何设置A的键??它太复杂了,我没有得到让我们假设我的所有数据都在appDelegate.ad中,其中ad是nsmutablery然后???@prajakta,是的,在重新加载表中的数据之前,您可以访问appDelegate nsmutablery变量。如何为??什么是mIndexedRecords和record是如此复杂??mIndexedRecords只是IndexedRecords,请使用此词典填充您的表视图。在CellForRowatinexPath数据源方法中,正如我在前面的一条评论中所说的。对不起!我说调用方法SetupRecords在numberOfSectionsInTableView数据源中进行索引。噢,我不能调用numberOfSectionsInTableView中的方法:(让我看看,试试别的:(哦,天哪,什么是mIndexedRecords和record??mIndexedRecords只是IndexedRecords,请使用此词典填充表视图。在cellForRowAtIndexPath数据源方法中,正如我在前面的一条评论中所说的。对不起!我说要调用numberOfSectionsInTableView数据源中的SetupRecords ForIndexing方法。哦,我无法在内部调用方法。)表格视图中的数字部分。:(让我看看试试其他东西:(哦,上帝