在搜索栏中搜索

时间:2014-04-23 19:39:39

标签: ios uitableview uisearchdisplaycontroller

我尝试使用searchBar进行在线搜索,我接下来会这样做:

//-=-=-=-=Methods for search Bar=-=-=-=
-(void)searchThroughData {

    self.result=nil;

    //Send searched substrint ti server
    if (self.searchBar.text.length > 0) {

        NSString *searchParams = [NSString stringWithFormat:@"<request<user_name>%@</user_name></request>", self.searchBar.text];

        [self initRequest:searchParams];
    }
}


-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    [self searchThroughData];
}
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=


- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    NSString *dataString = [[NSString alloc] initWithData:_receivedData encoding:NSUTF8StringEncoding];

    // -=-=-=-=-=-=-=-=-=-=Parse the XML into a dictionary-=-=-=-=-=-=-=-=-=-=
    NSError *parseError = nil;
    _xmlDictionary= [XMLReader dictionaryForXMLString:dataString error:&parseError];

    NSDictionary * dict=[_xmlDictionary objectForKey:@"result"];
    NSDictionary *dict1 = [dict valueForKey:@"user"];

    _result = [[NSMutableArray alloc] initWithArray:[[dict1 valueForKey:@"name"] valueForKey:@"text"]];

      NSLog(@"res: %@", _result);

       [self.tableView reloadData];

}


#pragma mark - Table view data source

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

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

    /*        !!!This never get called!!!
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        NSLog(@"in search");
    }
     */

     if(!self.searchDisplayController.isActive) {
        return myCategories.count;
    }
    else
    {
         NSLog(@"in search === %d", _result.count);
        return _result.count;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }


    if (!self.searchDisplayController.isActive) {
        cell.textLabel.text = myCategories[indexPath.row];
        return cell;
    }
    else {
        NSLog(@"search cllFrRwAt %@", _result[indexPath.row]);
        cell.textLabel.text = _result[indexPath.row];
        return cell;
    }
}

我可以在控制台中看到“在搜索=== 5”和“搜索cllFrRwAt john”(5次),但在屏幕上我看到“没有结果”。只有当我点击searchBar上的取消按钮时,我才会看到我下载的结果。如何在下载数据并更新searchDisplay时立即显示下载结果?

修改

我用:

更改了searchThroughData方法
-(void)searchThroughDataWithString: (NSString *)searchingStr {

…

}

[[NSOperationQueue mainQueue] addOperationWithBlock:^ {

                NSLog(@"Main Thread Code");
                [self.tableView reloadData];

            }];

但这没有帮助

0 个答案:

没有答案