1

Here's where the magic isn't happening:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath    *)indexPath
{
static NSString *CellIdentifier = @"songCell";
songViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

// Configure the cell...

long row = indexPath.row;

if (cell == nil) {
    cell = [[songViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

if (tableView == self.searchDisplayController.searchResultsTableView) {
    cell.songLabel.text = _searchResults[row];
} else {
    cell.songLabel.text = _songListArray[row];
}

return cell;
}

I know the _searchResults array is populated with the correct search results and I've edited the numberOfRowsPerSection appropriately. The filter is working correctly, but it won't display the _searchResults[row] text while typing into the search bar. If I don't use the bar, the cells are populated correctly with _songListArray[row].

Something is going wrong in:

if (cell == nil) {
    cell = [[songViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

If I don't include the if expression, I get an error. How should I initialize the prototype cell while the search bar is in use? All I get is empty labels, but I know the search is working because if there are no filtered results in the array the table says "No Results" in the middle. Why isn't my songLabel.text updating?? It won't even display text when I set the label to @"HELLO??" instead of the array[row].

mannibis
  • 27
  • 8

1 Answers1

1

You need to have the searchResultsTableView dequeue a cell, not just your main table view. The cellForRowAtIndexPath method should look something like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.tableView) {
        RDTriangleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
        cell.textLabel.text = self.theData[indexPath.row];
        return cell;
    }else{
        UITableViewCell *cell = [self.searchDisplayController.searchResultsTableView dequeueReusableCellWithIdentifier:@"SearchCell" forIndexPath:indexPath];
        //RDCell *cell = [self.searchDisplayController.searchResultsTableView dequeueReusableCellWithIdentifier:@"SearchCell" forIndexPath:indexPath];
        cell.textLabel.text = self.filteredData[indexPath.row];
        return cell;
    }
}

In viewDidLoad, you should register the class if you use the code I show above for a standard UITableViewCell.

[self.searchDisplayController.searchResultsTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"SearchCell"];

If you use a custom cell for the search results table, then you should use something like the line I have commented out, and register the nib for that custom cell.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • I will try that out! Thanks! Before I do so and to avoid further questions: Can I use the same prototype cell identifier for both my original table view and searchable table view? I notice you use "Cell" for the original and "searchCell" for the searchable table view. I have a prototype cell with a label in it that I want to use for both. Is that possible? – mannibis Dec 28 '13 at 03:25
  • Tried everything and I got: reason: 'unable to dequeue a cell with identifier songCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard' I am using the same selector (songCell), but I didn't register the nib for that custom cell. I assumed since the original table view loaded fine without registering the nib, I could do the same with the searchable table view. Sorry I am new to Objective-C and I feel like I'm asking a dumb question. – mannibis Dec 28 '13 at 03:41
  • I registered the custom class I'm using with: [self.searchDisplayController.searchResultsTableView registerClass:[songViewCell class] forCellReuseIdentifier:@"songCell"]; in the ViewDidLoad as you said, with the same Selector. No error this time, but still the labels are blank. – mannibis Dec 28 '13 at 03:49
  • @mannibis, I've tried it in the past using the same cell for both and ran into trouble. Did you try it like I showed -- just registering a UITableViewCell? – rdelmar Dec 28 '13 at 04:34
  • Yes, but as expected, the songLabel doesn't exist in that superclass, just in my songCellView subclass where I have the UILabel outlet. I guess I have to programmatically create a label and place it in the searchResultsTableView view. How would I even do that? – mannibis Dec 28 '13 at 04:43
  • I created a label programmatically and added the subView to the cell's ContentView. It works now!!! Thanks! Only thing is I have to clear the results each time I finish searching because it shows cells on top of each cell every time I change the search. Solved one problem, created another. I love programming! – mannibis Dec 28 '13 at 05:08
  • @mannibis, you wouldn't have to add any label -- a standard UITableViewCell has a label, called textLabel. – rdelmar Dec 28 '13 at 05:14
  • wow..I really feel dumb now. That solved my previous issue. So simple. Thanks again!! – mannibis Dec 28 '13 at 05:26