SearchBar 的觸發事件


//  kvo   kvc  

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //初始化指示器
    HUDManager = [[MBProgressHUDManager alloc] initWithView:self.navigationController.view];
    
    [self _initView];
    [self initNormalData1];
    
    //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(searchKeywordChanged) name:@"SearchKeywordChanged" object:nil]; //  不同於 kVo , kvc  這是通知中心
    //[self addObserver:self forKeyPath:@"keyword" options:0 context:nil];
    
    //[_MySearchBar addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
    
}
//
//-(void)didChangeValueForKey:(NSString *)key
//{
//    [self loadDataWithKeyWord:_keyword AndPage:1];
//}

//-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
//{
//    NSDictionary *change1 = change;
//    NSString *keyword = _keyword;
//    
//    
//    [self loadDataWithKeyWord:_keyword AndPage:1];
//    
//}

#pragma mark UISearchBarDelegate method

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText  // called when text changes
{
    if (![searchText isEqualToString:_oldSearchStr]) {
        
        [_diseaseModelsArray removeAllObjects];
        [self loadDataWithKeyWord:searchText AndPage:1 ];
        
        _oldSearchStr = searchText;
        
        _footerView.hidden = _diseaseModelsArray.count <= SearchNumber ? YES:NO;
        
    }
    
//    static int i = 1;
//    
//    if (i %2 == 0) {
//        
//        [_diseaseModelsArray removeAllObjects];
//        
//        [self loadDataWithKeyWord:searchText AndPage:1 ];
//        
//    }
//    
//    i++;
    
}


//-(BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
//{
//
//    if (![_keyword isEqualToString:_MySearchBar.text]) {
//        
//        [_diseaseModelsArray removeAllObjects];
//
//        [self loadDataWithKeyWord:_MySearchBar.text AndPage:1 ];
//        //_keyword = _MySearchBar.text;
//    }
//    
//    return YES;
//}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText  // called when text changes
{
    NSString *oldText = searchText;
    NSString *search = searchText;
    NSLog(@"///%@///%@////",searchBar.text,searchText);
    
    static int i = 1;
    
    if (i %2 == 0) {
        
        [_diseaseModelsArray removeAllObjects];
        
        [self loadDataWithKeyWord:searchText AndPage:1 ];
        
        //_keyword = searchText;
    }
    
    i++;
    
//    if ([searchText isEqualToString:@""]) {
//        //[_diseaseModelsArray removeAllObjects];
//        //[self initNormalData1];
//        return;
//    }
//    
//    if (![_keyword isEqualToString:searchText]) {
//        
//        
//    }
    
    //
    //[NSNotificationCenter defaultCenter] addObserver:<#(NSObject *)#> forKeyPath:<#(NSString *)#> options:<#(NSKeyValueObservingOptions)#> context:<#(void *)#>
    
    //[NSNotificationCenter defaultCenter] postNotificationName:<#(NSString *)#> object:<#(id)#> userInfo:<#(NSDictionary *)#>
}


<span style="font-family:SimHei;color:#ff6666;">//數據請求成功</span>
-(void)requestDidFinished:(ASIFormDataRequest *)request
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    [self.hudManager hide];
    
    NSDictionary *objDic = [NSJSONSerialization JSONObjectWithData:request.responseData options:NSJSONReadingMutableContainers error:nil];
    
    int code = [[objDic objectForKey:@"code"] intValue];
    
    //數據請求成功
    if (40000 == code)
    {
        if (![[objDic objectForKey:@"list"] isKindOfClass:[NSNull class]]) {
            
            for (NSDictionary *dic in [objDic objectForKey:@"list"]) {
                Disease *disease = [[Disease alloc] initWithDict:dic];
                [_diseaseModelsArray addObject:disease];
            }
            
        }
        
        [_MyTableView reloadData];
        
        [HUDManager hide];
        
    }
    
}



 <span style="font-size:18px;">   關於<span style="white-space:pre">	</span>searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText 的調用,要注意,每次輸入字符串,改方法都會調用兩次,一次輸出原始字符串,一次輸出更新後的字符串。所以如果不判斷就加載數據會導致數據清除不掉,因爲加載數據操作是異步操作,不在主線程中執行,很有可能</span><pre name="code" class="objc"><span style="font-size:18px;">_diseaseModelsArray沒有 remove 掉,就重新加了數據。</span>







發佈了68 篇原創文章 · 獲贊 10 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章