gpt4 book ai didi

ios - 通过在另一个 View 中更改设置来搜索 NSFetchedResults

转载 作者:行者123 更新时间:2023-11-29 02:31:59 25 4
gpt4 key购买 nike

我已经阅读了很多内容并观看了一些关于如何使用搜索栏搜索核心数据的教程,但是我还没有看到任何关于如何通过从 Settings 传递设置来更改排序描述符的内容 View 到 TableView

我有一个搜索栏按钮项,按下后会转到SearchSettingsVC。 View 通过将 bool 值从一个 VC 传递到另一个 VC 来进行通信,反之亦然。问题是通过更改 TableVC 顺序,表没有相应地对应 - (我尝试调用 self.tableview beginUpdates、self.tableview reload、self fetchedResultsController 等)。

重点是对 TableVC 结果重新排序,而不是像谓词那样仅呈现特定结果

我为 SettingsVC 创建了一个委托(delegate),这样我就可以将 bool 值传递给 SettingsVC,如果有任何更改,它就能够返回不同的更改值。

问题是我无法设法更新 TableView (或者甚至是获取的结果)。

这是我的 -(NSFetchedResultsController*) fetchedResultsController 方法的代码:

// return if already initialized
if (self.fetchedResultsController != nil) {
return self.fetchedResultsController;
}

if (dateSearch == true){

NSManagedObjectContext *managedObjectContext = [self managedObjectContext];

// the entity to fetch
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Details" inManagedObjectContext:managedObjectContext];
// how to sort the data
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"addDate" ascending:YES];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
[request setSortDescriptors:[NSArray arrayWithObject:sort]];

self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:managedObjectContext
sectionNameKeyPath:nil
cacheName:nil];

// fetch the data
NSError *e = nil;
if (![self.fetchedResultsController performFetch:&e]) {
NSLog(@"fetch error(Date): %@", e );
abort();
}

NSLog(@"Dates loaded");
}

if (mostAmount == true){

NSManagedObjectContext *managedObjectContext = [self managedObjectContext];

// the entity to fetch
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Details" inManagedObjectContext:managedObjectContext];
// how to sort the data
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"amount" ascending:NO];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
[request setSortDescriptors:[NSArray arrayWithObject:sort]];

self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:managedObjectContext
sectionNameKeyPath:nil
cacheName:nil];

// fetch the data
NSError *e = nil;
if (![self.fetchedResultsController performFetch:&e]) {
NSLog(@"fetch error (Most Fuel): %@", e);
abort();
}

NSLog(@"Amount loaded");

}

else{
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];

// the entity to fetch
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Details" inManagedObjectContext:managedObjectContext];
// how to sort the data
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"addDate" ascending:YES];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
[request setSortDescriptors:[NSArray arrayWithObject:sort]];

self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:managedObjectContext
sectionNameKeyPath:nil
cacheName:nil];

// fetch the data
NSError *e = nil;
if (![self.fetchedResultsController performFetch:&e]) {
NSLog(@"fetch error(Date): %@", e );
abort();
}

NSLog(@"Defualt loaded");
}

return self.fetchedResultsController;

我需要一个默认值,因为在开始时,当我将 TableVC 中的 bool 值初始化为 false 时,当我转到我的设置 VC 时,它们可以被更新。

我的 SearchSettingsVC 具有将值从 true 更改为 false(反之亦然)的 UISwitch,当我返回 SearchSettingsVC 时,它成功地更新了 TableVC 中表示的等效 bool 值。

我的 TableVC 中的 prepareForSegue 代码

if ([[segue identifier] isEqualToString:@"searchSettings"]){
//pass new search settings in here
SearchSelectionSettings * settingsVC = (SearchSelectionSettings *)segue.destinationViewController;

settingsVC.delegate = self;
settingsVC.dateSearch = dateSearch;
settingsVC.mostAmount = mostAmount;

这是我的 closeSettings 方法,位于我的 TableVC 中

#pragma mark - SettingsViewControllerDelegate methods
//record the settings changed in the settings view
//dismissViewController changes the screen
- (void)closeSettings:(id)sender {

NSLog(@"Working?");

dateSearch = ((SearchSelectionSettings *)sender).dateSearch;
mostAmount = ((SearchSelectionSettings *)sender).mostAmount;


[self dismissViewControllerAnimated:YES completion:nil];
[self FetchedResultsController];
NSIndexPath * indexPath;
[self tableView:self.tableView cellForRowAtIndexPath:indexPath];
}

将 bool 值从一个 VC 传递到另一个 VC 效果非常好。让 TableView 根据 fetchedResults if 语句更新,我在 SettingsVC 中的开关每次都会更新,没有任何问题。任何人都可以帮助或推荐教程吗?

最佳答案

fetchedResultscontroller getter 的开始行,即:

// return if already initialized
if (self.fetchedResultsController != nil) {
return self.fetchedResultsController;
}

意味着,一旦你的 fetchedResultsController 被创建,当你访问它时,剩下的代码将不会被执行。因此,解决问题的快速方法是在 closeSettings 方法中将 self.fetchedResultsController 设置为 nil,然后重新加载 TableView 。当 tableview 重新加载时,它会再次访问 fetchedResultsController,因为它现在是 nil,上面的代码将被绕过,你的代码将被使用。

#pragma mark - SettingsViewControllerDelegate methods
//record the settings changed in the settings view
//dismissViewController changes the screen
- (void)closeSettings:(id)sender {

NSLog(@"Working?");

dateSearch = ((SearchSelectionSettings *)sender).dateSearch;
mostAmount = ((SearchSelectionSettings *)sender).mostAmount;


[self dismissViewControllerAnimated:YES completion:nil];
self.fetchedResultsController = nil;
[self.tableView reloadData];
}

或者,您可以修改 fetchedResultsController 的 fetch,然后让它重新执行 fetch:

#pragma mark - SettingsViewControllerDelegate methods
//record the settings changed in the settings view
//dismissViewController changes the screen
- (void)closeSettings:(id)sender {

NSLog(@"Working?");

dateSearch = ((SearchSelectionSettings *)sender).dateSearch;
mostAmount = ((SearchSelectionSettings *)sender).mostAmount;


[self dismissViewControllerAnimated:YES completion:nil];
NSFetchRequest *request = [[NSFetchRequest fetchRequestWithEntityName:@"Details"];
NSSortDescriptor *sort;
if (dateSearch == true){
sort = [NSSortDescriptor sortDescriptorWithKey:@"addDate" ascending:YES];
NSLog(@"Dates loaded");
} else if (mostAmount == true) {
sort = [NSSortDescriptor sortDescriptorWithKey:@"amount" ascending:NO];
NSLog(@"Amount loaded");
} else {
sort = [NSSortDescriptor sortDescriptorWithKey:@"addDate" ascending:YES];
NSLog(@"Default loaded");
}
[request setSortDescriptors:[NSArray arrayWithObject:sort]];
self.fetchedResultsController.fetchRequest = request;
// fetch the data
NSError *e = nil;
if (![self.fetchedResultsController performFetch:&e]) {
NSLog(@"fetch error (Most Fuel): %@", e);
abort();
}
[self.tableView reloadData];
}

这样,您可以简化 fetchedResultsController 代码,使其只加载默认值。

关于ios - 通过在另一个 View 中更改设置来搜索 NSFetchedResults,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26752281/

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