2012-10-30 4 views
1

취소 버튼이있는 UISearchbar을 만들었지 만 취소 버튼을 클릭하면 배열이 표시되지 않고 키보드가 사라집니다.UISearchbar의 취소 버튼

allItemsNSArray하고 displayItems 당신은 정말이 두 배열을 사용한다고 NSMutableArray

-(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]; 
} 
+0

이 –

+0

더 많은 정보를 공유하시기 바랍니다 내가 편집 여기에 거친 예입니다 토픽 – ffds

+0

에있는 코드에서 취소 버튼을 클릭하면 어떤 메소드가 실행됩니까? –

답변

1

입니다. NSArray는 originalData와 같은 것을 호출하고, 다른 하나는 filteredData라는 NSMutableArray를 호출합니다. 이것들은 처음부터 동일 할 것이고, 필터링 할 때 originalData 배열로부터 filteredData 배열을 빌드/재 빌드 할 것입니다.

-(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]; 
} 

나는 또한 ScrollView (TableView)에 대한 대리자 메서드를 추가 시작을 스크롤 할 때, 키보드를 기각 가도록 (듯이) :

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

감사합니다 사람이 검색 응용 프로그램을 클릭했을 때이 검은 그림자가 나타납니다. – ffds