簡體   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