根据数据对数组进行排序

Jon*_*son 0 sorting objective-c nsarray xml-parsing ios

我有一个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;
}
Run Code Online (Sandbox Code Playgroud)

Eri*_*ric 6

你应该做的不是在cellForRow中对单元格进行排序,而是应该提前对数组进行排序然后调用[tableview reloadData].

有许多方法可以对数组进行排序,并且在不知道有关对象的所有信息的情况下,按日期排序的方法如下.

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"inputtime" ascending:TRUE];
[myMutableArray sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
Run Code Online (Sandbox Code Playgroud)