对 IBAction 上的 NSFetchedResultsController 进行排序

发布于 2025-01-02 12:02:31 字数 1275 浏览 1 评论 0原文

我想在我的表格视图中对数据进行排序。我有带有 IBAction 的 UIButtons 来调用排序。
我创建了一个包含 sortKey 的字符串。我正在设置键并再次调用 fetchedResultsController,对表视图进行排序。

问题是,未调用 fetchedResultsController 方法,并且排序不起作用。
这是我的代码:

- (IBAction) actionSortCardColor:(id) sender {
    XLog(@"");
    sortString = @"colorOrder";
    [self fetchedResultsController];

    [self actionRemoveSortView:sender];
}    

这是我的 fetchedResultsController 方法:

- (NSFetchedResultsController *)fetchedResultsController
{

[...]


NSPredicate *inboxPred = [NSPredicate predicateWithFormat:@"archived == 0"];

// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
[fetchRequest setPredicate:inboxPred];

XLog(@"sortString: %@", sortString);
if (sortString == nil) {
    sortString = [[NSString alloc] initWithString:@"sortingOrder"];
}

NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"setTitle" ascending:YES] autorelease];
NSSortDescriptor *sortDescriptor2 = [[[NSSortDescriptor alloc] initWithKey:sortString ascending:YES] autorelease];
NSArray *sortDescriptors = [[[NSArray alloc] initWithObjects:sortDescriptor, sortDescriptor2, nil] autorelease];

[fetchRequest setSortDescriptors:sortDescriptors];

[...]
}  

i want to sort my Data in my Tableview. I have UIButtons with IBAction to call the sort.
I have created a string which contains the sortKey. I am setting the the key and call the fetchedResultsController again, to sort the tableview.

Problem is, fetchedResultsController method is not called and the sorting doesnt work.
here is my code:

- (IBAction) actionSortCardColor:(id) sender {
    XLog(@"");
    sortString = @"colorOrder";
    [self fetchedResultsController];

    [self actionRemoveSortView:sender];
}    

Here my fetchedResultsController method:

- (NSFetchedResultsController *)fetchedResultsController
{

[...]


NSPredicate *inboxPred = [NSPredicate predicateWithFormat:@"archived == 0"];

// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
[fetchRequest setPredicate:inboxPred];

XLog(@"sortString: %@", sortString);
if (sortString == nil) {
    sortString = [[NSString alloc] initWithString:@"sortingOrder"];
}

NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"setTitle" ascending:YES] autorelease];
NSSortDescriptor *sortDescriptor2 = [[[NSSortDescriptor alloc] initWithKey:sortString ascending:YES] autorelease];
NSArray *sortDescriptors = [[[NSArray alloc] initWithObjects:sortDescriptor, sortDescriptor2, nil] autorelease];

[fetchRequest setSortDescriptors:sortDescriptors];

[...]
}  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゝ偶尔ゞ 2025-01-09 12:02:31

您在哪里让表视图了解这些更改?您需要重新加载表视图实例的数据 [tableView reloadData];

当您将 __fetchedResultsController 设置为 nil 时,它无法使用任何更改更新 UITable 视图,因为它不在范围内。

Where is it that you let the table view know about these changes? You'll need to reload the data for your table view instance [tableView reloadData];

When you set the __fetchedResultsController to nil, it cannot update the UITable view with any changes because it isn't in scope.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文