开发者

sort data and display in UItableview on iphone

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

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 *)t开发者_Go百科ableView:(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];
} 

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

// UPDATES ///////////////

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

my INDEXRECORD is not showing correct value i can now divide the section based on ABCD but each sections are having same content , what i am doing wrong


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

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


While indexing your records, keep your records in a dictionary say IndexedRecords. Such that, for key "A" set the array of records begin with letter "A". And while populating the table view

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

IndexLetters is also an array containing your index letters for the current list.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜