2011-09-16 215 views
1

我必须打印NSMutableArray *lstAirports;排序按字母顺序排列和显示数组值

我对象的数组它打印所有记录在表格视图单元格

我要排序的字母顺序我的单元格的值。

如何做到这一点请一些帮助,没有给我按字母顺序排列

显示此代码,这是我的控制器类的代码

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    // Return the number of rows in the section. 
    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]; 
    } 
    airport *a=(airport*)[app.lstAirports objectAtIndex:indexPath.row]; 
    NSLog(@"str:%@",a); 
    cell.textLabel.text =a.Name; 
    cell.detailTextLabel.text=a.Code; 
    // Configure the cell... 

    return cell; 
} 
+0

邮编您的阵列'airport'类 – Nekto

回答

1

您可以轻松地排序NSMutableArray

NSSortDescriptor *nameSort = [NSSortDescriptor sortDescriptorWithKey:@"Name" ascending:YES]; 
[lstAirports sortUsingDescriptors:[NSArray arrayWithObject:nameSort]]; 
0

您需要订购从中填充表数组在这种情况下,查看app.lstAirportsThis会教你如何做到这一点。

1

IN viewWillAppear

NSSortDescriptor *aSortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Name" ascending:YES]; 
[app.lstAirports sortUsingDescriptors:[NSArray arrayWithObject:aSortDescriptor]]; 
[aSortDescriptor release]; 
0

排序使用NSSortDescriptor

NSSortDescriptor * sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"key_name" ascending:YES]; 
[yourArray sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];