UISearchBar取消按钮问题

在我的应用程序中,我有一个分段和索引的tableview。 我成功地设法实现了该tableview的UISearchBar。 它会在我开始在搜索框中输入时过滤表格视图。 但是我无法正确使用Xcode的取消按钮。 我需要取消按钮来终止整个搜索(过滤)过程。 当我点击取消按钮时,它隐藏了键盘并清除了搜索区域,但它并没有将桌面视图刷新为原始状态(如iPhone中的手机应用中的可搜索),我尝试了许多事情,但没有运气。 这里是我的代码的样子:

谁能告诉我我错过了什么? 非常感谢

#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

您需要正确重置您的数据源。 改变这个:

-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    self.searchBar.text = nil;
    [self.searchBar resignFirstResponder];
    [self.tableView reloadData];
}

至:

-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    self.searchBar.text = nil;
    [self.searchBar resignFirstResponder];
    [self updateTableData:@""];
}
链接地址: http://www.djcxy.com/p/44283.html

上一篇: UISearchBar cancel button issue

下一篇: iphone UISearchBar Done button always enabled