排序数据并在iPhone上的UItableview中显示 [英] sort data and display in UItableview on iphone

查看:53
本文介绍了排序数据并在iPhone上的UItableview中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此查询按ASC顺序获取数据从list_tbl中选择*,其中cat_id =%d按名称ASC排序

i am using this query to get the data by ASC order select * from list_tbl where cat_id=%d order by names ASC

现在我的表格视图显示了基于ASC顺序的数据然后我用它来获得字母A到Z的右边

now my table view shows data based on ASC order then i used this to get the letters from A to Z on right side

- (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,当用户单击右侧的任何字母时,它都应转到该节

now my problem is how to divide data based on section header like all A content goes to A and then B till Z and when user click on any alphabet on right side it should goes to that section

//更新//////////////

-(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划分该部分,但每个部分的内容都相同,我在做什么错

推荐答案

例如:说RecordsArray包含您的所有记录,现在在表视图数据源方法中 numberOfSectionsInTableView 调用另一种方法来对所有内容进行索引像下面的记录

for example: say RecordsArray contains your all the records, now in table view datasource method numberOfSectionsInTableView call one more method to index your all the records like below

-(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 的计数.我希望这能解决您的问题.

and in numberOfSectionsInTableView return the count of IndexLetters. I hope this will solve your problem.

这篇关于排序数据并在iPhone上的UItableview中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆