2012-01-03 19 views
0

私はコアデータからいくつかのレコードを取り出し、それをテーブルビューで表示しました。これらのレコードはすべて11のプロパティを持っています。今私のテーブルビューのプレゼンテーションでは、ヘッダーにこれらのプロパティすべてのボタンを追加しました。ボタンをクリックすると、そのプロパティに基づいてレコードをソートする必要があります。 ソート記述子を使用しようとしましたが、動作しません。 私のコードは次のようである: -NSManagedオブジェクト配列の並べ替え

- (IBAction)sortButton:(id)sender { 
// tags are from 100 to 111 
UIButton *sortButton = (UIButton*)sender; 
NSString *sortDescriptorKey = nil; 
// create sort descriptor key according to the tag value 
if (sortButton.tag == 100) sortDescriptorKey = [NSString stringWithString:@"reportID "]; 
.... 
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:sortDescriptorKey ascending:YES]; 
[self.recordArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]]; 
[sortDescriptor release]; 
[self.tableView reloadData]; 
} 

しかし、このコードはまったく順序を変更しません。 何かが欠けているのですか、それを行う別の方法がありますか?

答えて

1

問題が

[self.recordArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]]; 

この文にあるこれはrecordArrayのソートされたコピーを返しますが、あなたはそれを使用していません。再ロード時にtableViewが正しく更新されるように、recordArrayに割り当てる必要があります。それを次のように変更してください:

self.recordArray=[self.recordArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]]; 
+0

ありがとう、何か愚かな間違い:) – cocoaNoob