2012-12-13 62 views
0

我有一個tableview,用於顯示存儲在NSObject中的數組的數據。 NSObject中的一個屬性是inputtime(currentCall.inputtime),我想根據它顯示數據。我會如何去做這件事?謝謝。基於數據對數組進行排序

這裏是我的cellForRowAtIndexPath:

- (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

你要排序數據潛行第一。但是在方法didSelectRowforIndexPath之外。

在你的代碼中,你訪問:

currentCall = [[xmlParser calls] objectAtIndex 

此時陣列必須已經進行排序。

這樣排序,例如在ViewWillAppear。 在cellForRowAtIndexPath數據已經必須排序。

如果數據模型動態變化,例如通過刪除單元行,那麼你必須重新與你的表格數據[self.tableView reloadData]

+0

數據在數組之前被解析?我想要做的就是根據currentCall.inputtime排序xmlParser.calls數組。 –

+0

所有的[xmlParser調用]是,是一個數組,它不會執行分析或任何事情,如果這就是您的想法。 –

+0

我根據你的意見更新了 – AlexWien

相關問題