Мой tableView обновляется только из UISearchBar только после добавления дополнительного символа - PullRequest
1 голос
/ 23 ноября 2010

У меня есть tableView, который обновляется данными из массива через функцию textDidChange для UISearchBar. Данные верны, но они не отображаются в табличном представлении до тех пор, пока после нажатия дополнительного символа (например, если я набрал «Boise» в строке поиска, ничего не происходит, но «Boise1» загрузит два города с именем «Boise» «... так что это один шаг позади).

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{

    //---if there is something to search for---
    if ([searchText length] > 0) {

        NSLog(@"greater than 0!");

        isSearchOn = YES;
        canSelectRow = YES;
        self.tableView.scrollEnabled = YES;

        [self performSelector:@selector(someMethod) withObject:nil afterDelay:0];

        //[NSThread detachNewThreadSelector:@selector(matchSearchText)   
        //                 toTarget:self withObject:nil];

        //[self matchSearchText];
    }
    else {
        //---nothing to search---
        isSearchOn = NO;
        canSelectRow = NO;
        self.tableView.scrollEnabled = NO;      
    }
}

и в вызываемом методе:

    - (void) someMethod {
    NSLog(@"-------------");
    NSLog(@"some Method whut!");

    CityLookup *cityLookup = [[CityLookup alloc] findCity:searchBar.text];

    // clear array
    [tableCities removeAllObjects];

    if ([cityLookup.citiesList count] > 0) {
        tableCities = cityLookup.citiesList;
        int size = [tableCities count];
        NSLog(@"there are %d objects in the array", size);
    }

    [cityLookup release];

    [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:tableCities waitUntilDone:YES];

}

и, наконец, cellForRowAtIndexPath:

-(UITableViewCell *)tableView:(UITableView *)tableView
    cellForRowAtIndexPath:(NSIndexPath *)indexPath {

int size = [tableCities count];
NSLog(@"there are %d objects in the array NOW", size);

static NSString *kCellID = @"cellID";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
if (cell == nil)
{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID] autorelease];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}


    NSString *cellValue = [tableCities objectAtIndex:indexPath.row];

    cell.textLabel.text = cellValue;
    return cell;

}
...