首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UISearchBarDelegate和UITableView不能一起工作吗?

UISearchBarDelegate和UITableView不能一起工作吗?
EN

Stack Overflow用户
提问于 2012-03-16 22:17:20
回答 3查看 218关注 0票数 0

我有一个带有标准UISearchBar的表视图控制器

我希望在键入文本时将表中的第一行替换为搜索栏中的文本。我以为这会行得通,但它不是:

代码语言:javascript
复制
- (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];
}

如果我将结果数组记录到控制台,它会正确地替换对象,但表视图不会拾取它。

我照常设置了表数据源:

代码语言:javascript
复制
- (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;
}

如果我只使用insertObjectAtIndex:或insertObject:,它确实会显示结果。只是当我试图替换第一个对象时不会。请帮帮忙!:)

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-03-16 23:27:17

好吧,我想明白了。也真的很愚蠢...searchBar覆盖了tableView的第一行,所以我看不到它在更新。多伊尔。

票数 0
EN

Stack Overflow用户

发布于 2012-03-16 22:26:18

尝试删除这一行:if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; }。我这么说的原因是你在分配每个单元格。

这是我藏起来的一篇文章:iPhone: How to purge a cached UITableViewCell

票数 -1
EN

Stack Overflow用户

发布于 2012-03-16 22:57:12

您可以尝试重新加载搜索表视图,如下所示:

代码语言:javascript
复制
[self.searchDisplayController.searchResultsTableView reloadData];
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9738926

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档