繁体   English   中英

如何在iPhone中按字母顺序排列

[英]how to give section alphabetical order in iphone

我在appdelegate类中有表和一个全局变量。 我将所有日期存储在此全局数组中,并在表视图的控制器中显示此数组值。 我必须按字母顺序简短地给段索引赋值,请给我帮助,我尝试了此代码,但它对我不起作用请帮助我。
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;
        }

    }

完美的例子,请参考代码, 按字母顺序排序

为了按字母顺序显示/排列数据,您必须使用NSSortDescriptor在数组中

您必须创建此NSSortDescriptor类的对象,并在此处提供要从XML提取的数据

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

现在假设您有一个数组sortDescriptors

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


[yourArray sortUsingDescriptors:sortDescriptors];

希望对您有用。

如果您要按节进行排序,我会将您的各个节保存在一个字典中,该字典中包含每个节的数组,然后根据需要对每个数组进行排序。 它需要在表委托方法上添加更多代码,但可以完全控制每个节的数据

另一个选择是将数据封装在不同的容器中,并为所需的节添加一个属性,并为其添加另一个排序描述符,以便它按节然后按任何其他值对数组进行排序。 您只需按照要对数组进行排序的顺序,将另一个NSSortDescriptor添加到数组即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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