2011-09-16 213 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]];