UISearch Bar in ios 8

52 Views Asked by At

I am getting JSONArray data and show phone numbers in table
I am using UISearchBar in UITableView.

When I am type in search bar it reload data and show type value in first cell.

But i clicked first cell means it show old array value.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
   if (isSearching) 
   {
        cell.textLabel.text = [searchResultArray objectAtIndex:indexPath.row];  
   }
   else 
   {
        cell.textLabel.text = [phoneNoArray objectAtIndex:indexPath.row];
   }
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText 
{
   [searchResultArray removeAllObjects];
   if([searchText length] != 0)
   {
        isSearching = YES;
        [self searchTableList];
   }
   else 
   {
        isSearching = NO;
   }
   [self.tableView reloadData];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    tempString = [jsonArray objectAtIndex:indexPath.row];

}
1

There are 1 best solutions below

0
Misha On

when you set isSearching to yes. Now datasoucre of table has changed. and you have reloaded the data.

and you are supposed to update your datasource methods as well

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (isSearching) {
    return [searchResultArray count];
}
else
{
  return [phoneNoArray count];
 }
}

HTH