[英]UISearchBar cancel button issue
in my app i have a sectioned and indexed tableview. 在我的应用程序中,我有一个分区和索引的tableview。 i successfully managed to implement a UISearchBar to that tableview.
我成功地实现了对该表视图的UISearchBar。 it filters the table view when i start to type into the search box.
当我开始在搜索框中输入内容时,它会过滤表格视图。 however i could not be able to use Xcode's Cancel button properly.
但是我无法正确使用Xcode的“取消”按钮。 I need the cancel button to terminate the whole search(filter) process.
我需要取消按钮来终止整个搜索(过滤器)过程。 when i hit cancel button it hides the keyboard and clears the search area but it does not refresh the tableview to its original state.(like the searchable in phone app in iPhones) i tried numerous things but no luck.
当我按下“取消”按钮时,它隐藏了键盘并清除了搜索区域,但并没有将Tableview刷新到其原始状态。 here is how my code looks like:
这是我的代码的样子:
can anyone tell me what i am missing? 谁能告诉我我在想什么? thanks a lot
非常感谢
#import "TableViewController.h"
#import "DetailViewController.h"
#import "Songs.h"
@implementation TableViewController
@synthesize allTableData;
@synthesize filteredTableData;
@synthesize letters;
@synthesize searchBar;
@synthesize isFiltered;
NSArray *SongsIndexTitles;
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *newButton = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
[[self navigationItem] setBackBarButtonItem:newButton];
SongsIndexTitles = @[@"A", @"B", @"C",@"Ç", @"D", @"E", @"F", @"G", @"H", @"I",@"İ", @"J", @"K", @"L", @"M", @"N", @"O", @"Ö", @"P", @"R", @"S",@"Ş", @"T", @"U",@"Ü", @"V", @"Y", @"Z"];
searchBar.delegate = (id)self;
allTableData = [[NSArray alloc] initWithObjects:
[[Songs alloc] initWithName:@"Adam" andDescription:@"sanatcı 1"],
[[Songs alloc] initWithName:@"Anam" andDescription:@"sanatcı 2"],
[[Songs alloc] initWithName:@"Babam" andDescription:@"sanatcı 1"],
[[Songs alloc] initWithName:@"Burcu" andDescription:@"sanatcı 1"],
[[Songs alloc] initWithName:@"Cemil" andDescription:@"sanatcı 1"],
[[Songs alloc] initWithName:@"Cemal" andDescription:@"sanatcı 1"],
.
.
.
[[Songs alloc] initWithName:@"Yeşim" andDescription:@"sanatcı 1"],
[[Songs alloc] initWithName:@"Yaşar" andDescription:@"sanatcı 1"],
[[Songs alloc] initWithName:@"Ziya" andDescription:@"sanatcı 1"],
[[Songs alloc] initWithName:@"Ziban" andDescription:@"sanatcı 1"],
nil ];
[self updateTableData:@""];
}
- (void)viewDidUnload
{
[self setSearchBar:nil];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
#pragma mark - Table view data source
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString* key = [letters objectAtIndex:section];
return key;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return letters.count;
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return SongsIndexTitles;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSString* letter = [letters objectAtIndex:section];
NSArray* arrayForLetter = (NSArray*)[filteredTableData objectForKey:letter];
return arrayForLetter.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
NSString* letter = [letters objectAtIndex:indexPath.section];
NSArray* arrayForLetter = (NSArray*)[filteredTableData objectForKey:letter];
Songs* songs = (Songs*)[arrayForLetter objectAtIndex:indexPath.row];
cell.textLabel.text = songs.name;
cell.detailTextLabel.text = songs.description;
CGSize itemSize = CGSizeMake(50, 50);
UIGraphicsBeginImageContextWithOptions(itemSize, NO, UIScreen.mainScreen.scale);
CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
[cell.imageView.image drawInRect:imageRect];
cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return cell;
return cell;
}
-(void)updateTableData:(NSString*)searchString
{
filteredTableData = [[NSMutableDictionary alloc] init];
for (Songs* songs in allTableData)
{
bool isMatch = false;
if(searchString.length == 0)
{
isMatch = true;
}
else
{
NSRange nameRange = [songs.name rangeOfString:searchString options:NSCaseInsensitiveSearch];
NSRange descriptionRange = [songs.description rangeOfString:searchString options:NSCaseInsensitiveSearch];
if(nameRange.location != NSNotFound || descriptionRange.location != NSNotFound)
isMatch = true;
}
if(isMatch)
{
NSString* firstLetter = [songs.name substringToIndex:1];
NSMutableArray* arrayForLetter = (NSMutableArray*)[filteredTableData objectForKey:firstLetter];
if(arrayForLetter == nil)
{
arrayForLetter = [[NSMutableArray alloc] init];
[filteredTableData setValue:arrayForLetter forKey:firstLetter];
}
[arrayForLetter addObject:songs];
}
}
letters = [[filteredTableData allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
[self.tableView reloadData];
}
#pragma mark - Table view delegate
-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text
{
[self updateTableData:text];
}
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
[self.searchBar setShowsCancelButton:YES animated:YES];
return YES;
}
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
[self.searchBar setShowsCancelButton:NO animated:YES];
return YES;
}
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
self.searchBar.text = nil;
[self.searchBar resignFirstResponder];
[self.tableView reloadData];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"ShowDetails"]) {
DetailViewController *destinationViewController = segue.destinationViewController;
UITableViewCell *selectedCell = [self.tableView cellForRowAtIndexPath:self.tableView.indexPathForSelectedRow];
destinationViewController.title = selectedCell.textLabel.text;
}
}
@end
You need to reset your data source properly. 您需要正确重置数据源。 Change this:
更改此:
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
self.searchBar.text = nil;
[self.searchBar resignFirstResponder];
[self.tableView reloadData];
}
to: 至:
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
self.searchBar.text = nil;
[self.searchBar resignFirstResponder];
[self updateTableData:@""];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.