2013-08-07 57 views
1

我要与数据源中的所有数据加载搜索显示控制器的表视图用户开始搜索前前SearchDisplayController的数据显示。可以开始搜索

我用

[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; 
} 
+0

查看cellForRowAtIndexPath的代码: –

+0

检查您的搜索结果为空或不是 –

回答

0

可能是你在“SearchResult所”阵列,同时在委托方法添加数据...

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString 
你的情况

你需要在搜索显示控制器的表全部数据鉴于没有搜索可能无法拨打上面的委托方法和你的数组没有数据... 所以你需要“SearchResult所”阵列搜索显示控制器的表视图的重载之前添加数据。

[self.searchDisplayController.searchResultsTableView reloadData]; 
+0

我在searchResults数组中手动​​添加数据,然后重新载入tableView。我验证在SearchResult所包含的数据调试模式 – Shuaib

+0

确实在寻找的UISearchBar代码正确的工作.. – Dafda

+0

是代码工作搜索 – Shuaib

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

所以,如果它不为你工作,我想你有另一个错误在别的地方。

+0

没有工作.....我不知道为什么....理论是和该问题的原因是在你链接后正确解释.... – Shuaib

+0

添加我使用的代码的作品编辑。 – DCMaxxx

+0

它不适用于我....现在我这样做**没有**搜索显示控制器,我得到的结果 – Shuaib