簡體   English   中英

根據數據對數組進行排序

[英]Sort array based on data

我有一個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;
}

您應該做的是代替對cellForRow中的單元格進行排序,而應該提前對數組進行排序,然后調用[tableview reloadData]

有很多方法可以對數組進行排序,而在不了解有關對象的所有信息的情況下,按日期排序的一種方法如下。

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

您必須首先對數據模型進行排序。 但是在方法didSelectRowforIndexPath

您可以在代碼中訪問的位置:

currentCall = [[xmlParser calls] objectAtIndex

此時,該數組必須已經排序。

ViewWillAppear中進行排序。 cellForRowAtIndexPath ,數據必須已排序。

如果數據模型動態更改(例如,通過刪除單元格行),那么您必須使用[self.tableView reloadData]定義表數據

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM