UISearchBar の textDidChange 関数を介して配列からのデータを更新する tableView があります。データは正しいですが、追加の文字を入力するまで tableView に表示されません (たとえば、検索バーに「Boise」と入力しても何も起こりませんが、「Boise1」は「Boise」という名前の 2 つの都市を読み込みます'..だから一歩遅れている)。
- (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;
}