0

I have a table view controller with a standard UISearchBar

I want to replace the first row in the table with the text in the search bar as it gets typed. I thought this would work but it doesn't:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    // results is a mutable array with an empty string at index 0
    [[self results] replaceObjectAtIndex:0 withObject:searchText];   
    [[self tableView] reloadData];
}

If I log the results array to the console it is correctly replacing the object, but it is not getting picked up by the table view.

I have the table data source set up as per usual:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[self results] count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault     reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...

    [[cell textLabel] setText:[[self results] objectAtIndex:[indexPath row]]];

    return cell;
}

If I just use insertObjectAtIndex: or insertObject: it does show up the results. Just not when I'm trying to replace first object. Help please! :)

Cameron
  • 4,181
  • 9
  • 36
  • 40
  • Try killing this line: `if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; }` . The reason I say this is that you are alloc-ing each cell. – Gobot Mar 16 '12 at 14:47
  • Here's a post that I had stashed away: http://stackoverflow.com/questions/2286669/iphone-how-to-purge-a-cached-uitableviewcell – Gobot Mar 16 '12 at 14:50

2 Answers2

0

Ok figured it out. Really stupid too.... The searchBar was covering the first row of the tableView, so I couldn't see it updating. Doh.

Cameron
  • 4,181
  • 9
  • 36
  • 40
-1

Try killing this line: if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } . The reason I say this is that you are alloc-ing each cell.

Here's a post that I had stashed away: iPhone: How to purge a cached UITableViewCell

Community
  • 1
  • 1
Gobot
  • 2,464
  • 1
  • 21
  • 26