我有一个带有标准UISearchBar的表视图控制器
我希望在键入文本时将表中的第一行替换为搜索栏中的文本。我以为这会行得通,但它不是:
- (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];
}如果我将结果数组记录到控制台,它会正确地替换对象,但表视图不会拾取它。
我照常设置了表数据源:
- (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:,它确实会显示结果。只是当我试图替换第一个对象时不会。请帮帮忙!:)
发布于 2012-03-16 23:27:17
好吧,我想明白了。也真的很愚蠢...searchBar覆盖了tableView的第一行,所以我看不到它在更新。多伊尔。
发布于 2012-03-16 22:26:18
尝试删除这一行:if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; }。我这么说的原因是你在分配每个单元格。
这是我藏起来的一篇文章:iPhone: How to purge a cached UITableViewCell
发布于 2012-03-16 22:57:12
您可以尝试重新加载搜索表视图,如下所示:
[self.searchDisplayController.searchResultsTableView reloadData];https://stackoverflow.com/questions/9738926
复制相似问题