简体   繁体   English

UISearchbar上的“取消”按钮

[英]Cancel button on UISearchbar

I've created a UISearchbar with a cancel button, but when I click on the cancel button, it does not show the array and it just dismisses the keyboard. 我创建了带有取消按钮的UISearchbar ,但是当我单击取消按钮时,它不显示数组,而只是关闭了键盘。

allItems is NSArray and displayItems is NSMutableArray allItemsNSArraydisplayItemsNSMutableArray

-(void)searchBarSearchButtonClicked:(UISearchBar *)asearchBar{
[displayItems addObject:allItems];
[searchBar resignFirstResponder];
 }


-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{

if ([searchText length] == 0) {
    [displayItems removeAllObjects];
    [displayItems addObjectsFromArray:allItems];

} else {

    [displayItems removeAllObjects];
    for (NSString * string in allItems ){
        NSRange r =[string rangeOfString:searchText options:NSCaseInsensitiveSearch];

        if (r.location != NSNotFound){
            [displayItems addObject:string];
        }
    }

    [tableView reloadData];

} 

 }


       - (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellAccessoryDisclosureIndicator;
}

 -(void)searchBarCancelButtonClicked:(UISearchBar *)asearchBar{
[displayItems removeAllObjects];
[displayItems addObjectsFromArray:allItems];
[searchBar resignFirstResponder];
}
-(void)searchBarSearchButtonClicked:(UISearchBar *)asearchBar{
[searchBar resignFirstResponder];
}

You really ought to be using two Arrays for this. 您确实应该为此使用两个数组。 One an NSArray called something like originalData and the other an NSMutableArray called filteredData. 一个NSArray称为原始数据,另一个NSMutableArray称为filteredData。 These will both be the same in the beginning and when filtering, you will build/rebuild the filteredData array from the originalData array. 它们在开始时都是相同的,并且在过滤时,您将从原始Data数组构建/重建filteredData数组。 Here is a rough example: 这是一个粗略的示例:

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{

    if ([searchText length]) {
        [displayItems removeAllObjects];
        for (NSString * string in allItems ){
            NSRange r =[string rangeOfString:searchText options:NSCaseInsensitiveSearch];

            if (r.location != NSNotFound){
                [displayItems addObject:string];
            }

        [tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
        }
    } 
}



-(void)searchBarCancelButtonClicked:(UISearchBar *)asearchBar{
    [searchBar resignFirstResponder];
    self.displayItems = [[NSMutableArray alloc] initWithArray:allItems];
    [tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO]; 
}

I also add a delegate method for the ScrollView ( TableView ) so that when scrolling starts, the keyboard gets dismissed: 我还为ScrollViewTableView )添加了一个委托方法,以便当滚动开始时,键盘被关闭:

#pragma mark - ScrollView (UITableView) delegate methods
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [mySearchBar resignFirstResponder];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM