A couple of questions on searchDisplayController

Discussion in 'iOS Programming' started by Dookieman, May 29, 2014.

  1. Dookieman macrumors 6502

    Joined:
    Oct 12, 2009
    #1
    In my app I have a search function that loads some JSON data from a site, works great. I originally set up using the the standard "Subtitle" type of cell that Apple provides but I've had to move to a custom cell because I've added a button to each IndexRow which didn't seem to work with Apple's template.

    Now when I search, the searchDisplayController TableView no longer has any data inside of it, just blank cells. Once I hit cancel, The table view located behind it includes everything from the custom cell I made. My question is, how can I display the information from my custom cell in my searchDisplayController TableView?

    Also, I would like to add the feature that it begins searching once the user starts to enter in test in the UITextField. Is there a delegate method for this?

    Also, I would like the to close the searchDisplayController tableview when the search button is click and just have the results populated in my custom tableview.

    Thanks, if you need any additional information let me know and I can provide it.
     
  2. TheWatchfulOne, May 29, 2014
    Last edited: May 29, 2014

    TheWatchfulOne macrumors 6502

    TheWatchfulOne

    Joined:
    Jun 19, 2009
    #2
    Have you confirmed your search is returning any results?

    Presumably you have a method you call that performs the search. Call that method every time the user types a new character into the search bar. Check the UISearchBarDelegate protocol documentation to see all the available methods from which you can call your search method.

    Call
    Code:
    resignFirstResponder
    on the keyboard when the user touches anything other than the keyboard or search bar.

    Showing some code would help.
     
  3. Dookieman thread starter macrumors 6502

    Joined:
    Oct 12, 2009
    #3
    Yes, data is loading. I can do multiple searches and the appropriate data is displayed in the TableView behind the searchDisplayController.

    I'll look into searchbarDelegate protocol. Thanks!

    This is the code that displays info into my TableView with the custom cells.
    Code:
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        // Return the number of sections.
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        // Return the number of rows in the section.
        return [[SharedInfo sharedUserInfo].userSearchArray count];
        NSLog(@"Table Loaded");
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        
        static NSString *cellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        
        
        if (cell==nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
        }
        
        NSDictionary *tempDictionary = [[SharedInfo sharedUserInfo].userSearchArray objectAtIndex:indexPath.row];
        
        UILabel *gameTitleLabel = (UILabel*)[cell viewWithTag:1];
        gameTitleLable.text = [tempDictionary objectForKey:@"title"];
    
        UILabel *systemTitleLabel = (UILabel *)[cell viewWithTag:2];
        systemTitleLable.text = [tempDictionary objectForKey:@"system_title"];
    This is the code that begins the search and used to load the data into searchDisplayController TableView, until I switched to a custom cell.

    Code:
    ConnectionDataRequest *newSearch = [[ConnectionDataRequest alloc] init];
    [newSearch loadUsersInfo:request success:^{
            dispatch_async(dispatch_get_main_queue(), ^{
                [[[self searchDisplayController] searchResultsTableView] performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
                [self.tableView reloadData];
    }
    }failure:^{
            NSLog(@"Data Load Failed");
        }];
    }
     
  4. TheWatchfulOne macrumors 6502

    TheWatchfulOne

    Joined:
    Jun 19, 2009
    #4
    Part of your
    Code:
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    method got cut off. I don't see a place where you are declaring or using a custom cell.

    Also, I don't see your search method. By search method I mean a method where you iterate through your main data array and place items that match your search criteria into a search results array.

    The UITableViewDatasource method:
    Code:
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    has an input parameter, tableView.

    Check tableView to see whether it is equal to self.tableView or your seach bar tableview. Your table cells for self.tableView should get their values from the data objects in your main data array. Your search results tableview cells should get their values from data objects in your search results data array.

    Can we see the rest of your
    Code:
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    method?
     
  5. Dookieman thread starter macrumors 6502

    Joined:
    Oct 12, 2009
    #5
    My apologies! Must have missed the end of it. Though there wasn't much I missed.

    Code:
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        // Return the number of sections.
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        // Return the number of rows in the section.
        return [[SharedInfo sharedUserInfo].userSearchArray count];
        NSLog(@"Table Loaded");
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        
        static NSString *cellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        
        
        if (cell==nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
        }
        
        NSDictionary *tempDictionary = [[SharedInfo sharedUserInfo].userSearchArray objectAtIndex:indexPath.row];
        
        UILabel *gameTitleLabel = (UILabel*)[cell viewWithTag:1];
        gameTitleLable.text = [tempDictionary objectForKey:@"title"];
    
        UILabel *systemTitleLabel = (UILabel *)[cell viewWithTag:2];
        systemTitleLable.text = [tempDictionary objectForKey:@"system_title"];
    
        return cell;
    }
    
     
  6. TheWatchfulOne macrumors 6502

    TheWatchfulOne

    Joined:
    Jun 19, 2009
    #6
    Ok.

    Is that everything? Can you share your method that iterates though your data array and places search results into a search results array?

    I noticed something else:

    Code:
    UILabel *gameTitleLabel = (UILabel*)[cell viewWithTag:1];
        gameTitleLable.text = [tempDictionary objectForKey:@"title"];
    
        UILabel *systemTitleLabel = (UILabel *)[cell viewWithTag:2];
        systemTitleLable.text = [tempDictionary objectForKey:@"system_title"];
    
        return cell;
    You are creating UILabels, but you never set the labels frames
    (unless viewWithTag: returns a View object with a set frame) and you never add them as subViews to your cell.

    You should initialize them with a frame:

    Code:
    UILabel *_myLabel = [[UILabel alloc] initWithFrame: _myLabelFrame];
    And add them to your cell as a subView:

    Code:
    [cell addSubview: gameTitleLabel];
     
  7. Dookieman thread starter macrumors 6502

    Joined:
    Oct 12, 2009
    #7
    I got it!

    I added this and it worked!

    Code:
    if (tableView == self.searchDisplayController.searchResultsTableView) {
            cell.textLabel.text = [tempDictionary objectForKey:@"title"];
            cell.detailTextLabel.text = [tempDictionary objectForKey:@"system_title"];
        }
        else {
    
        UILabel *gameTitleLable = (UILabel*)[cell viewWithTag:1];
        gameTitleLable.text = [tempDictionary objectForKey:@"title"];
        
        UILabel *systemTitleLable = (UILabel *)[cell viewWithTag:2];
        systemTitleLable.text = [tempDictionary objectForKey:@"system_title"];
        }
    Thanks!
     

Share This Page