SearchDisplayController可以在开始搜索之前显示数据

时间:2013-08-07 05:19:27

标签: ios objective-c uitableview uisearchdisplaycontroller

我想在用户开始搜索之前,使用数据源中的所有数据加载搜索显示控制器的表格视图

我用过

[self.searchDisplayController.searchResultsTableView reloadData];

在viewDidLoad中,但是UITableViewDataSource委托方法

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

没有被调用。我的桌子一直空着。 我在重新加载表格视图之前手动填充数据源(并在调试模式下验证了这一点),因此有数据用于填充单元格。

我知道我可以使用另一个表视图执行此操作并直观地获得完全相同的结果但我想知道是否只能使用搜索显示控制器的表视图。

代码:

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

    static NSString *cellIdentifier = @"Cell";

    cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if(cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }


    NSString *category = [searchResults objectAtIndex:[indexPath row]];
    cell.textLabel.text = category;

    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.textLabel.numberOfLines = 0;
    cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;


    return cell;
}

2 个答案:

答案 0 :(得分:1)

您可能需要查看this SO question。从来没有尝试过。

告诉我它是否有帮助; - )

编辑:

在您发表评论之后,我尝试了答案,它对我来说很好(仍然看到了暗淡的观点,正如他在答案中所说的那样。但是没有尝试将其删除)。

以下是我在我的应用程序中尝试的代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    _places = @[ [[RHPlace alloc] initWithName:@"name" address:nil distance:nil andFoursquareUID:@"plop"] ];
    [self.searchDisplayController.searchResultsTableView reloadData];
    CGRect testFrame = CGRectMake(0, 20, self.searchDisplayController.searchBar.frame.size.width, 100);
    self.searchDisplayController.searchResultsTableView.frame = testFrame;
    [self.searchDisplayController.searchBar.superview addSubview:self.searchDisplayController.searchResultsTableView];
}

以下是我得到的结果:http://cl.ly/image/0k0V3J3H1G1h

因此,如果它对您不起作用,我猜您在其他地方还有另一个错误。

答案 1 :(得分:0)

可能是您在'searchResults'数组中添加数据而委托方法...

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString

在你的情况下你需要在搜索显示控制器的表视图中没有搜索的整个数据可能不会调用上面的委托方法,你的数组没有数据... 所以你需要在重新加载搜索显示控制器的表视图之前在'searchResults'数组中添加数据。

[self.searchDisplayController.searchResultsTableView reloadData];