gpt4 book ai didi

ios - 搜索栏取消按钮不重新加载原始 TableView 和数据

转载 作者:行者123 更新时间:2023-11-29 12:02:10 24 4
gpt4 key购买 nike

搜索和其他一切工作正常,除了当我在搜索栏上单击取消时,会出现一个空屏幕,上面有空搜索栏和原始 TableView 的导航栏。来自 Core Data 的原始、未过滤的数组不会重新加载。我已经检查了 Storyboard 中的“显示取消按钮”。我研究了许多帖子和文章,但没有任何建议奏效。控制台消息显示,在按下“取消”按钮后,在其他消息中,我看到“获取后的事件:”执行了两次。第二次(最后一次)它包含我所有的托管对象/事件,但随后“过滤数组:”为空,“事件:”为空,“events.self.count:”= 0,searchBarText 为空,self .events.count = 0.

EventViewController.h 文件:

    #import <UIKit/UIKit.h>

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@interface EventViewController: UITableViewController <UISearchResultsUpdating, UISearchControllerDelegate, UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate>


{
IBOutlet UISearchBar *searchBar;
}

@end

EventViewController.m 文件:

#import "EventViewController.h"
#import <CoreData/CoreData.h>
#import "EventDetailViewController.h"

@interface EventViewController ()

@property (nonatomic, strong) NSMutableArray *events;
@property (nonatomic, strong) UISearchController *searchController;
@property(nonatomic, weak) id< UISearchResultsUpdating > searchResultsUpdater;
@property (nonatomic,strong) UISearchBar *_searchBar;
@property(nonatomic, assign) BOOL obscuresBackgroundDuringPresentation;
@property(nonatomic, weak) id< UISearchControllerDelegate > delegate;

//@property(nonatomic, assign, getter=isActive) BOOL active;

@end


@implementation EventViewController
NSMutableArray *events;


- (NSManagedObjectContext *)managedObjectContext {

NSLog(@"Setting up managed object context");

NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;

}



- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{

// Fetch the events from persistent data store

NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Event"];

self.events = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];


NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", self.searchController.searchBar.text];

self.events = [[self.events filteredArrayUsingPredicate:resultPredicate] mutableCopy];


}


- (BOOL)shouldReloadTableForSearchString:(UISearchController *)searchController


{


return YES;

}



- (void)viewDidLoad {
[super viewDidLoad];

self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = false;
self.tableView.tableHeaderView = self.searchController.searchBar;
self.definesPresentationContext = true;
self.searchController.searchBar.delegate = self;
self.searchController.delegate = self;
self.searchController.obscuresBackgroundDuringPresentation = NO;

}



- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{

[self.searchController.searchBar becomeFirstResponder];
}




- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}



- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];

// Fetch the events from persistent data store
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Event"];
self.events = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];

[self.tableView reloadData];
}




- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{

self.searchController.searchBar.text = nil;
[self.searchController.searchBar resignFirstResponder];
[self.tableView reloadData];

}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {


return 1;
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {


{

return [self.events count];
}
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{


static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];


// Configure the cell...
NSManagedObject *event = [self.events objectAtIndex:indexPath.row];
[cell.textLabel setText:[NSString stringWithFormat:@"%@", [event valueForKey:@"name"]]];

{

event = [events objectAtIndex:indexPath.row];
NSManagedObject *event = [self.events objectAtIndex:indexPath.row];
[cell.textLabel setText:[NSString stringWithFormat:@"%@", [event valueForKey:@"name"]]];
}

return cell;

}



// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}



// Override to support editing the table view.

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSManagedObjectContext *context = [self managedObjectContext];

if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete object from database
[context deleteObject:[self.events objectAtIndex:indexPath.row]];

NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]);
return;
}

// Remove event from table view
[self.events removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}


#pragma mark - Navigation


// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)send


{

if ([[segue identifier] isEqualToString:@"UpdateEvent"]) {
NSManagedObject *selectedEvent = [self.events objectAtIndex:[[self.tableView indexPathForSelectedRow] row]];

EventDetailViewController *destViewController = segue.destinationViewController;
destViewController.event = selectedEvent;
}
}

@end

最佳答案

如果你正在调用取消函数你不应该过滤数组,所以你不应该执行我下面提到的行,为此你可以检查 self.searchController.searchBar.text< 的长度 或者您可以设置一个 bool 值来检查是否单击了取消或在文本字段中输入了文本

  NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", self.searchController.searchBar.text];

self.events = [[self.events filteredArrayUsingPredicate:resultPredicate] mutableCopy];

关于ios - 搜索栏取消按钮不重新加载原始 TableView 和数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36438573/

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