简体   繁体   中英

how to give section alphabetical order in iphone

I have table and one global variable in appdelegate class. I store all date in this global array and showing this array value in controller in table view. I have to give section index with alphabetical order by short how to give please help me on this I tried this code but it not working for me Kindly help me.
h.file :

NSMutableArray *timeZonesArray;
NSMutableArray *sectionsArray;
UILocalizedIndexedCollation *collation;

.mfile

    #pragma mark -
    #pragma mark Table view data source

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        // Return the number of sections.
        //return 1;
        return [[collation sectionTitles] count];
    }


    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        // Return the number of rows in the section.

            //app.networkActivityIndicatorVisible = YES;
        NSArray *timeZonesInSection = [app.lstAirports objectAtIndex:section];

        return [timeZonesInSection count];


       // return app.lstAirports.count;

    }


    // Customize the appearance of table view cells.
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
        }

        NSArray *timeZonesInSection = [app.lstAirports objectAtIndex:indexPath.section];


         a=(airport*)[timeZonesInSection objectAtIndex:indexPath.row];
        NSLog(@"str:%@",a);
        if(a!=nil){
        cell.textLabel.text =a.Name;
        cell.detailTextLabel.text=a.Code;
            //[app.spinner stopAnimating];
        }
        return cell;
    }




    #pragma mark -
    #pragma mark Table view delegate


    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
        return [[collation sectionTitles] objectAtIndex:section];
    }


    - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
        return [collation sectionIndexTitles];
    }


    - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
        return [collation sectionForSectionIndexTitleAtIndex:index];
    }



    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    {
        app.originAirport = (airport*)[app.lstAirports objectAtIndex:indexPath.row];
       [delegate Originstart:app.originAirport  forIndexPath:self.indexPathToChange];
        [self.navigationController popViewControllerAnimated:YES];
    }


    #pragma mark -
    #pragma mark Set the data array and configure the section data

    - (void)setTimeZonesArray:(NSMutableArray *)newDataArray {
        if (newDataArray != timeZonesArray) {
            [timeZonesArray release];
            timeZonesArray = [newDataArray retain];
        }
        if (timeZonesArray == nil) {
            self.sectionsArray = nil;
        }

    }

The perfect example , please refer the code, Sorting Alphabetically

In order to display/arrange your data in alphabetical which in a array you have to use NSSortDescriptor

you have to make the object of this NSSortDescriptor class and give data here which you are fetching from XML

NSSortDescriptor *itemXml = [[NSSortDescriptor alloc] initWithKey:@"itemName" ascending:YES];

Now suppose you have an array sortDescriptors

NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:itemXml,nil];


[yourArray sortUsingDescriptors:sortDescriptors];

I hope this will be usefull to you.

If you are looking for section-wise sorting I would hold your various sections in a dictionary that holds arrays for each of the sections and then sort each of those arrays as needed. It requires a bit more code on the table delegate methods but allows for complete control over each section's data

Another option is to encapsulate your data in a different and add a property for the section you want it in and add another sort descriptor to that so it is sorting the array by section and then by any other value. You do that by just adding another NSSortDescriptor to the array in the order you want the array to be sorted by.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM