2

我正在尝试直接从 UISearchBar 搜索。我遇到了这个问题,想知道是否有任何教程可以解决这个问题,或者是否有人可以提供帮助。这是我的代码:

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

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

    } else {
        [displayItems removeAllObjects];

        NSString *url=[NSString stringWithFormat:@"http://xml.customweather.com/xml?client=clinique_test&client_password=f@c3$toF&product=search&search=%@",searchBar.text];
        NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0];

        }
    }

    // reload the table data
    [self.tableView reloadData];
} 
4

2 回答 2

3

尝试这个

 - (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text 
    {
        NSString *trimDot = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

        if ([trimDot isEqualToString:@"."]) {
            return YES;
        }

    if(connection){
        [connection cancel];
    }
    NSString *appendStr;
       if([text length] == 0)
    {
        NSRange rangemak = NSMakeRange(0, [searchbar.text length]-1);
        appendStr = [searchbar.text substringWithRange:rangemak];
    }
    else
    {
        appendStr = [NSString stringWithFormat:@"%@%@",searchbar.text,text];

    }
    [self performSelector:@selector(callSearchWebService:) withObject:appendStr];

    [activityindicator startAnimating];
    return YES;
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    [self performSelector:@selector(callSearchWebService:) withObject:searchBar.text];
    searchBar.showsCancelButton=NO;

}

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

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{   
    [self performSelector:@selector(callSearchWebService:) withObject:searchBar.text];
    [searchBar resignFirstResponder];
}

-(void)callSearchWebService:(NSString*)searchStr
{

        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;

        NSString *str =@"";
        if ([searchStr length] != 0 ) str = searchStr;


        NSString *url = [NSString stringWithFormat:@"%@/%d/%d/%@/",ListoutForSearcing_server,minRecords,maxRecords,str];
        [request setURL:[NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];

        [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

        // Create Connection.
        connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

        if (connection) {
            JsonResponseData = [NSMutableData data] ;
            NSLog( @"Data will be received from URL: %@", request.URL );
        }
        else
        {// The download could not be made.
            NSLog( @"Data could not be received from: %@", request.URL );
        }
    HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
    [self.navigationController.view addSubview:HUD];

    HUD.delegate = self;
    HUD.labelText = @"Loading";
    [HUD show:YES];
}

连接委托方法

#pragma mark Connection delegate methods.
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [JsonResponseData setLength:0];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [JsonResponseData appendData:data];   

}

-(void)connection:(NSURLConnection*)connection didFailWithError:(NSError *)error
{
    UIAlertView *alertError=[[UIAlertView alloc]initWithTitle:@"Message" message:@"Network Problem." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [alertError show];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    receivedString = [[NSString alloc] initWithData:JsonResponseData 
                                           encoding:NSASCIIStringEncoding];
    RecievedDataDict=[[NSMutableDictionary alloc]init];

}
于 2013-01-10T12:47:44.633 回答
0

从您的代码中,您可以URLConnection在每个字符更改中使用它,UISearchBar因此它对您来说并不完美。

笔记

在这里,您想从网络服务器获取数据,然后在UISearchBar.. 的文本字段中输入整个字符串后发送请求,就像在searchBarSearchButtonClicked方法中一样。

//RootViewController.m
- (void) searchBarSearchButtonClicked:(UISearchBar *)theSearchBar {

   [self searchTableView];
   [searchTableView reloadData];
}

- (void) searchTableView {

    NSString *url = [NSString stringWithFormat:@"http://xml.customweather.com/xml?client=clinique_test&client_password=f@c3$toF&product=search&search=%@",searchBar.text];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0];
}
于 2013-01-10T12:41:24.097 回答