UISearchBar取消按钮问题

时间:2015-06-09 22:23:46

标签: ios objective-c uitableview uisearchbar cancel-button

在我的应用程序中,我有一个分区和索引的tableview。我成功地设法将UISearchBar实现到该tableview。当我开始在搜索框中输入时,它会过滤表格视图。但是我无法正确使用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

1 个答案:

答案 0 :(得分:0)

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

-(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:@""];
}
相关问题