Sort data and view in UItableview on iphone

advertisements

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

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.