2014-04-12 5 views
0

UITableView에 UISearchBar를 추가하려고하지만 TextDidBeginEditing 메서드가 호출되지 않습니다. 나는 내가 간단한 것을 놓치고 있다고 생각하지만 그것을 이해할 수는 없다.UISearchBar이 UITableView를 필터링하지 않습니다.

헤더 파일 :

@interface TableViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate> 

@property (nonatomic, strong) NSMutableArray *initialCities; 
@property (nonatomic, strong) NSMutableArray *filteredCities; 
@property (nonatomic, strong) UISearchBar *searchBar; 
@property BOOL isFiltered; 

@end 

실행 파일 :

#import "TableViewController.h" 

@interface TableViewController() 

@end 

@implementation TableViewController 

@synthesize initialCities, filteredCities, isFiltered, searchBar; 

- (void)loadView 
{ 
    [super loadView]; 
    searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 44.0)]; 
    searchBar.delegate = self; 
    searchBar.autoresizingMask = (UIViewAutoresizingFlexibleWidth); 
    searchBar.autocorrectionType = UITextAutocorrectionTypeNo; 
    self.tableView.tableHeaderView = searchBar; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    self.tableView.delegate = self; 

    initialCities = [[NSMutableArray alloc] initWithObjects:@"London", @"New York", @"Berlin", nil]; 
} 

#pragma mark - UITableView DataSource Methods 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

    if (isFiltered == YES) 
    { 
     return filteredCities.count; 
    } 
    else 
    { 
     return initialCities.count; 
    } 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
    } 

    if (isFiltered == YES) 
    { 
     cell.textLabel.text = [filteredCities objectAtIndex:indexPath.row]; 
    } 
    else 
    { 
     cell.textLabel.text = [initialCities objectAtIndex:indexPath.row]; 
    } 

    return cell; 
} 

#pragma mark - UITableView Delegate Methods 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

} 

#pragma mark - UISearchBar Delegate methods 

- (void)searchBar:(UISearchBar *)searchBar TextDidBeginEditing:(NSString *)searchText 
{ 
    searchBar.showsCancelButton = YES; 

    if (searchText.length == 0) 
    { 
     isFiltered = NO; 
    } 
    else 
    { 
     isFiltered = YES; 

     filteredCities = [[NSMutableArray alloc] init]; 

     for (NSString *cityName in initialCities) 
     { 
      NSRange cityNameRange = [cityName rangeOfString:searchText options:NSCaseInsensitiveSearch]; 

      if (cityNameRange.location != NSNotFound) 
      { 
       [filteredCities addObject:cityName]; 
      } 
     } 
    } 

    [self.tableView reloadData]; 

} 

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar 
{ 
    [searchBar resignFirstResponder]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 

결과는 검색 막대가 표시되는 테이블 뷰가 표시되지만 내가 검색에 텍스트를 입력 할 때 바 있다는 것입니다 테이블보기가 필터링되지 않습니다. 충돌은 없습니다. 어떤 도움을 주셔서 감사합니다. 고맙습니다.

+0

... –

답변

1

당신이 구현 방법은 프로토콜에조차 존재하지 않는이

-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)searchText 
{  
    // Your filter code 
} 

을 시도

– searchBar:textDidChange: 
– searchBar:shouldChangeTextInRange:replacementText: 
– searchBarShouldBeginEditing: 
– searchBarTextDidBeginEditing: 
– searchBarShouldEndEditing: 
– searchBarTextDidEndEditing: 
+1

예이했다. 원래 textDidChange를 가졌지 만, 다른 이유가있을 때는 작동하지 않았습니다. 나는 그것을 다시 시도 했어야했다. 많은 감사합니다 !! – pjv

0
  • (무효) 검색 창은 : (UISearchBar *) 검색 창 TextDidBeginEditing은 (는 NSString *)는
  • 을 검색 텍스트

이 메서드는 searchBar의 대리자에는 없습니다.

searchBarTextDidBeginEditing을 사용하십시오.

0

통해 이동 tutorial. 잘 설명했다. 희망 당신은 ARC를 사용하는

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope 
{ 
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", searchText]; 
    searchResults = [actualData filteredArrayUsingPredicate:resultPredicate]; 
} 
관련 문제