2012-12-13 3 views
0

NSObject에 저장된 배열의 데이터를 표시하는 tableview가 있습니다. NSObject의 속성 중 하나는 inputtime (currentCall.inputtime)이며이를 기반으로 표시된 데이터를 정렬하려고합니다. 이 일을 어떻게 하죠? 감사. 당신은 당신의 데이터가 먼저 MODELL 정렬이데이터를 기반으로 정렬 정렬

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    JointCAD *currentCall = [[xmlParser calls] objectAtIndex:indexPath.row]; 
    static NSString *cellidentifier1 = @"cell1"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellidentifier1]; 

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellidentifier1]; 
    } 

    cell.textLabel.text = currentCall.currentCallType; 
    cell.detailTextLabel.text = currentCall.location; 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

    return cell; 
} 

답변

6

으로 테이블 데이터를 다시 정렬해야합니다. cellForRow에서 셀을 정렬하는 대신 미리 배열을 정렬 한 다음 [tableview reloadData]을 호출해야합니다.

배열을 정렬하는 데는 여러 가지 방법이 있으며 개체에 대한 모든 정보를 모르는 상태에서 날짜순 정렬 방법 중 하나는 다음과 같습니다.

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"inputtime" ascending:TRUE]; 
[myMutableArray sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]]; 
1

:

여기 내 cellForRowAtIndexPath입니다. 그러나 방법 didSelectRowforIndexPath 외부.

코드의 일부 어디에 당신이 액세스 :

currentCall = [[xmlParser calls] objectAtIndex 

배열이 이미 정렬되어 있어야합니다이 때.

예 : ViewWillAppear으로 정렬합니다. cellForRowAtIndexPath에서 이미 데이터를 정렬해야합니다.

데이터 모델이 동적으로 변경되는 경우. 셀 행을 삭제하면 [self.tableView reloadData]

+0

데이터가 배열에서 이전에 파싱 되었습니까? 모두 내가하고 싶은 일은 currentCall.inputtime을 기반으로 xmlParser.calls 배열을 정렬하는 것입니다. –

+0

[xmlParser calls]는 배열이며, 구문 분석을하지 않습니다. –

+0

귀하의 의견에 따라 업데이트되었습니다. – AlexWien

관련 문제