gpt4 book ai didi

ios - Core Data 中的数据集的顺序

转载 作者:行者123 更新时间:2023-11-29 03:32:06 24 4
gpt4 key购买 nike

我的应用程序从核心数据中检索一些数据,例如某人的姓名、生日、家庭住址等。

我的初始 View 计算人与人之间的距离,但我如何加载按降序排列的距离到表格 View 。

也许是个愚蠢的问题,但我试图使用 NS Mutable 字典来实现这一点,但不幸的是,我将键和值添加到 nsmutable 字典中,但无法将其加载到 TableView 中。关于如何让它工作或我应该使用什么来代替可变字典有什么想法吗?

最佳答案

我不确定我能否准确辨别您想要完成的任务,或者其中的哪一部分给您带来麻烦,但也许 sortUsingComparator:^() 方法NSMutableArray 可能会有所帮助。

这是一个简单的示例,通过比较每个元素的创建日期,使用它对名为 moviesOnlyNSMutableArray 进行排序。

//Order by NSURLCreationDateKey
[moviesOnly sortUsingComparator:^NSComparisonResult(NSURL* a, NSURL* b){
NSDate* a1 = [[a resourceValuesForKeys:[NSArray arrayWithObject:NSURLCreationDateKey] error:nil] objectForKey:NSURLCreationDateKey];
NSDate* b1 = [[b resourceValuesForKeys:[NSArray arrayWithObject:NSURLCreationDateKey] error:nil] objectForKey:NSURLCreationDateKey];
return [a1 compare:b1];
}
];

假设你 can well-order all of your elements那么像这样的比较器可以为你整理你的 NSMutableArray 。然后将 UITableViewDataSource 协议(protocol)方法指向已排序的 NSMutableArray。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//point the relevant property of each cell at the corresponding member of your now sorted NSMutableArray here
}

关于ios - Core Data 中的数据集的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19623744/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com