我的 tableView 仅在添加附加字符后才从 UISearchBar 刷新

发布于 2024-10-04 02:03:34 字数 2235 浏览 8 评论 0原文

我有一个 tableView,它通过 UISearchBar 的 textDidChange 函数刷新数组中的数据。数据是正确的,但直到点击附加字符后它才会出现在 tableView 中(因此,例如,如果我在搜索栏中输入“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;

}

I have a tableView that refreshes with data from an array via the textDidChange function for UISearchBar. The data is correct, but it does not appear in the tableView until after hitting an additional character (so, for instance, if i typed 'Boise' into the search bar, nothing happens, but 'Boise1' will load two cities named 'Boise'..so it's one step behind).

- (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;      
    }
}

and in the method that is called:

    - (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];

}

and finally the 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;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文