[英]How to add search bar with bar button item, I am using core data?
我需要搜索存储在UITableView
。 我需要一个左栏按钮项,当用户单击该按钮时,它应显示为搜索栏。 怎么做?。
提前致谢!
编辑:
确定基于您提供的解决方案。在这里,我已经完成了搜索栏(栏按钮项)的某些操作。 但我仍然缺少一些东西。 而且我的搜索框过滤器内容也有问题。 真的我现在不该做什么吗?
#import "ViewController.h"
#import "AddNoteViewController.h"
#import <CoreData/CoreData.h>
#import "TableViewCell.h"
@interface ViewController ()
{
NSArray *searchResults;
}
@property (nonatomic, strong) UISearchBar *searchBar;
@property (nonatomic, strong) UISearchDisplayController *searchController;
@property (nonatomic) BOOL *isChecked;
@property (strong) NSMutableArray *notes;
//@property (strong, nonatomic) NSArray *_lastIndexPath;
@end
@implementation ViewController
@synthesize tableView;
@synthesize addButton;
//@synthesize _lastIndexPath;
@synthesize managedObjectContext;
- (NSManagedObjectContext *)managedObjectContext {
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.title = @"My Notes";
tableView.dataSource = self;
tableView.delegate = self;
[self.view addSubview:tableView];
// place search bar coordinates where the navbar is position - offset by statusbar
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 20, 320, 44)];
//self.searchResults = [[NSArray alloc] init];
UIBarButtonItem *searchButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(toggleSearch)];
self.navigationItem.rightBarButtonItem = searchButton;
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsDelegate = self;
self.searchController.delegate = self;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Notes"];
NSError *error = nil;
self.notes = [[managedObjectContext executeFetchRequest:fetchRequest error:&error] mutableCopy];
NSSortDescriptor *titleSorter= [[NSSortDescriptor alloc] initWithKey:@"mod_time" ascending:NO];
[self.notes sortUsingDescriptors:[NSArray arrayWithObject:titleSorter]]
;
NSLog(@"Your Error - %@",error.description);
[tableView reloadData];
}
#pragma mark - Search controller
- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
{
}
- (void) searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller
{
}
- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
{
// [self.searchBar removeFromSuperview];
}
- (void)toggleSearch
{
[self.view addSubview:self.searchBar];
[self.searchController setActive:YES animated:YES];
[self.searchBar becomeFirstResponder];
}
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Notes" inManagedObjectContext:_notes];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"notes.title contains[c] %@", searchText];
[fetchRequest setPredicate:predicate];
NSError *error;
NSArray* searchResults = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
// return searchResults.count;
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [searchResults count];
} else {
return self.notes.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TableViewCell *cell = (TableViewCell*)[aTableView dequeueReusableCellWithIdentifier:@"MycellIdentifier"];
if(cell == nil)
{
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MycellIdentifier"];
}
_notes = [_notes objectAtIndex:indexPath.row];
// Configure the cell...
NSManagedObject *note = [self.notes objectAtIndex:indexPath.row];
NSDate *date = [note valueForKey:@"mod_time"];
cell.textLabel.text = [note valueForKey:@"title"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
- (IBAction)addButtonPressed:(id)sender {
AddNoteViewController *addNoteVC = [AddNoteViewController new];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
- (void)tableView:(UITableView *)cTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSManagedObjectContext *context = [self managedObjectContext];
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete object from database
[context deleteObject:[self.notes objectAtIndex:indexPath.row]];
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]);
return;
}
// Remove device from table view
[self.notes removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
-(UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
@end
使用数组NSMutableArray * tempArray;
将会有一些dataSource数组,您可以根据dataSource.count从中告诉tableView show numberOfRow ,并在cellForRow中显示该数组中包含的数据。 所以搜索你可以使用
NSString *searchTerm = @"E";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY SELF contain %@", searchTerm];//use whatever you want for ANY SELF and searchTerm according to your need
NSArray *filtered = [array filteredArrayUsingPredicate:predicate];
tempArray = [filtered mutableCopy];
并使用此tempArray在UITableView中显示。 谢谢。
如果我正确理解,您想将搜索栏添加到工具栏。 这是将UISearchbar作为条形按钮项添加到UIToolBar的方法:
// your searchbar
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(xposition, yposition, width, height)];
//create a customview and make its frame equal to your searchbar
UIView *searchBarView = [[UIView alloc] initWithFrame:searchBar.frame];
// add your searchbar to the custom view
[searchBarView addSubview:searchBar];
//finally add it to your bar item of the toolbar
UIBarButtonItem *searchBarItem =
[[UIBarButtonItem alloc] initWithCustomView:searchBarView];
// Now assign it to your left button item or wherever you want to add to toolbar.
如果您需要其他帮助,请提供更多详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.