2012-01-03 2 views
0

코어 데이터에서 일부 레코드를 가져 와서 테이블보기로 표시했습니다. 이 기록에는 모두 11 개의 속성이 있습니다. 이제 내 tableview 프레 젠 테이션에서 헤더에 이러한 모든 속성에 대한 버튼을 추가했습니다. 버튼을 클릭하면 해당 속성에 따라 레코드를 정렬하려고합니다. 정렬 설명자를 사용해 보았지만 작동하지 않습니다. 이 코드는 전혀 순서를 변경하지 않습니다,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