SearchBar Search Button Clicked Error

Discussion in 'iOS Programming' started by loon3y, Jun 15, 2012.

  1. loon3y macrumors 65816

    loon3y

    Joined:
    Oct 21, 2011
    #1
    Code:
    - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
    {
        NSLog(@"search text %@", searchText);
        
        userText = searchText;
    }
    
    - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
    {
        GlobalStrings* theDataObject = [self theGlobalClass];
        
        [status setString:@""];
        [status appendFormat:theDataObject.serviceURL];
        [status appendFormat:@"smartsearch?scode="];
        [status appendFormat:@"%@", userText];
        
        
        
        NSLog(@"value of the status is: %@", status );
        NSString *urlAddress = status;
        NSURL *url = [NSURL URLWithString:urlAddress];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
        
        if (connection)
        {
            self.responseData = [NSMutableData data];
        }
        else
        {
            NSLog (@"The connection failed");
        }
        
        // display the scanned data on top of screen
        [status setString:@""];
        
        [itemSearch resignFirstResponder];
    }
    

    on the
    Code:
    [status appendFormat:@"%@", userText];


    i get an exec_bad_access error, i have no idea what this means can anyone help explain?
     
  2. idelovski macrumors regular

    Joined:
    Sep 11, 2008
    #2
    Because searchText in userText is gone by then. This supposedly instance variable userText should be a property so you can do self.userText = searchText;

    Unless you're under arc and in that case you may have another problem.
     
  3. loon3y thread starter macrumors 65816

    loon3y

    Joined:
    Oct 21, 2011
    #3


    ic ic, that worked perfectly thank you,


    now Im trying to load my table view with the data i called from the web service,


    where would i reload the tableview data?

    in the search button clicked?
     
  4. idelovski macrumors regular

    Joined:
    Sep 11, 2008
    #4
    No but maybe yes.

    ;)

    I know I would call it there, just to remove old items before new search brings anything. Then later in NSURLConnectionDelegate again to populate the table with the fresh data.

    In my own project I have a search bar, but I am displayng data already in memory so I reload table after each key press. If you're dealing with responsive server and if you have enough time to do some experiments you can try comunicating with the server in -searchBar:textDidChange: just for fun of it.
     
  5. loon3y thread starter macrumors 65816

    loon3y

    Joined:
    Oct 21, 2011
    #5


    sorry i got confused with the command.



    my app has a lot of spaghetti programming, its really sloppy, but I'm just trying to get it work, and clean it up later.



    but basically I'm just trying to populate my search bar view, and I'm just using the same code basically that i used before (which was also a table view) to populate my search bar tableview (search results) but before it only returned one item, but now since it a search function, it returns more than one parsed item, does this matter? does it conflict?


    im definitely getting data back from the web service, as i checked in my debugger, its just not populating my tableview.


    heres my code, and if u want to and have the time, can u tell me whats wrong? i get a bad EXC_BAD_ACCESS error on this part of the code
    Code:
    searchData.searchCode = [NSMutableString stringWithString:searchViewItemNo];
    i also used a class and an array, because i have no idea how to count all my search results without them.


    Code:
    - (void)parseXML 
    {
        GlobalStrings* theDataObject = [self theGlobalClass];
        NSLog (@"parseXML");
        // Initialize the parser with our NSData from the RSS feed
        NSXMLParser *xmlParser = [[NSXMLParser alloc] 
                                  initWithData:self.responseData];
        
        // Set the delegate to self
        [xmlParser setDelegate:self];
        
        // Start the parser
        if (![xmlParser parse])
        {
            NSLog (@"An error occurred in the parsing");    
        }
        // Release the parser because we are done with it
        [xmlParser release];
        
        Searches *searchData = [[Searches alloc] init];
        searchData.searchCode = [NSMutableString stringWithString:searchViewItemNo];
        searchData.searchDescript = [NSMutableString stringWithString:searchViewDescript];
        searchData.searchOnHand = [NSMutableString stringWithString:searchViewOnHand];
        searchData.searchPrice = [NSMutableString stringWithString:searchViewPrice];
        
        
        [theDataObject.searchTableArray addObject:searchData];
        
    }

    added this just to make sure its fine (it worked on my other view controllers, but just to be safe)


    Code:
    - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
      namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
    {
        NSLog (@"didEndElement");
        // Check to see which element we have ended
        
        // If we are in an item and ended a title
        if ([elementName isEqualToString:@"onhand"]) 
        {
            searchViewOnHand =  capturedCharacters ;
            NSLog(@" onhand = %@", capturedCharacters);
        }
        if ([elementName isEqualToString:@"description"])
        {
            searchViewDescript = capturedCharacters;
            NSLog(@" descpt = %@", capturedCharacters);
        }
        if ([elementName isEqualToString:@"price"])
        {
            searchViewPrice = capturedCharacters;
        }
        if ([elementName isEqualToString:@"itemno"])
        {
            searchViewItemNo = capturedCharacters;
        }
        [capturedCharacters release];
        capturedCharacters = nil;
        
        if ([elementName isEqualToString:@"smartsearch"]) {
            // We are no longer in an item element
            inItemElement = NO;
        }
    }
    
    - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
    {
        if (capturedCharacters != nil) 
        {
            [capturedCharacters appendString:string];
        }
    }

    and heres how i set up my table



    Code:
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
         GlobalStrings* theDataObject = [self theGlobalClass];
        // Return the number of rows in the section.
        return [theDataObject.searchTableArray count];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
         GlobalStrings* theDataObject = [self theGlobalClass];
        static NSString *CellIdentifier = @"SearchCell";
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }
    
        
        // Configure the cell...
        
        Searches *mySearch = [Searches alloc];
        mySearch = [theDataObject.searchTableArray objectAtIndex:[indexPath row]];
        
        
        UILabel *itemLabel = (UILabel *) [cell viewWithTag:100];
        itemLabel.text = mySearch.searchCode;
        UILabel *descriptLabel = (UILabel *) [cell viewWithTag:101];
        descriptLabel.text = mySearch.searchDescript;
        UILabel *onHandLabel = (UILabel *) [cell viewWithTag:102];
        onHandLabel.text = mySearch.searchOnHand;
        UILabel *priceLabel = (UILabel *) [cell viewWithTag:103];
        priceLabel.text = mySearch.searchPrice;
        UIImageView *searchImageView = (UIImageView *) [cell viewWithTag:104];
        
        NSString *sURL = [[NSString alloc] initWithString:[NSString stringWithFormat:@"http://***************.com:90/prodimage/%@_t.jpg", searchViewItemNo]];
        
        NSData *myData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", sURL]]];
        
        UIImage *picture = [[UIImage alloc] initWithData:myData];
        [searchImageView setImage:picture];
        [searchImageView setNeedsDisplay];
        
        
        
        return cell;
    }
    


    i know its a lot of coding, but any quick insight, or anything that catches ur eye at first glimpse would be much appreciated.
     
  6. idelovski macrumors regular

    Joined:
    Sep 11, 2008
    #6
    I think you're making the same mistake as before, searchViewItemNo (and others) should be a property and it should start working as expected.
     
  7. loon3y thread starter macrumors 65816

    loon3y

    Joined:
    Oct 21, 2011
    #7
    i finally got something show up on the table view but it came out all funky like this

    [​IMG]


    i have a feeling its because theres two items when I'm parsing, or its because i might of allocated the array at the wrong spot. not sure why it comes out all messed up rather than the cells that i made for them.
     

Share This Page