6

我正在使用故事板。我有一个带有一个原型单元的 UITableView。此单元格的样式为“字幕”。我已经从单元格中添加了一个 segue 到详细视图。因此,当用户点击一个单元格时,它将打开相应的编辑器......这一切都很好。

现在我添加了一个 UISearchDisplayController 和一个 UISearchBar,实现了委托。这很好用。

但在搜索结果表中,单元格的样式为“默认”且不可点击。我必须做些什么才能让结果表看起来和表现得像“未搜索”表?

4

5 回答 5

24

我想为答案#1做出贡献,这就是我所做的,它对我有用

在方法中

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

而不是从参数 tableView 分配单元格

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

直接从视图上的 TableView 分配它,因此您必须替换它

// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

有了这个

 UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
于 2012-09-12T00:48:36.327 回答
8

发现问题...

方法

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

从 tableView 中拉出单元格,在结果情况下不是来自情节提要的 tableView,而是来自 SearchDisplayController 的 resultTableView。

我现在从情节提要的表格视图中让单元格在两种情况下都显示,现在它可以工作了。

于 2011-12-01T16:38:22.117 回答
3

我一直在使用 ios 7.0 和 Xcode 5.0。我发现搜索显示控制器使用与委托视图控制器相同的 tableview 布局。你所要做的就是判断当前的tableview是delegate view controller的tableview,还是搜索显示控制器的tableview。但是记得加一句

tableView.rowHeight = self.tableView.rowHeight;

在以下代码片段中:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if (tableView == self.searchDisplayController.searchResultsTableView)
{
    tableView.rowHeight = self.tableView.rowHeight;//very important!
    return [self.searchResults count];
}
else
{
    ...
    return ...;
}
}

如果你忘记实现那句话,那么搜索显示的表格视图的行只有默认行那么高,这会让你觉得它看起来不像“未搜索”的表格。

于 2014-01-14T12:40:27.157 回答
0

这里有一个可能的答案。它可能不完全适合您,但正如我所解释的, UISearchDisplayController 创建表视图。

检查文档,您可以更好地理解它,但它指出:

您使用搜索栏和视图控制器初始化搜索显示控制器,该控制器负责管理要搜索的原始内容。当用户开始搜索时,搜索显示控制器负责将搜索界面叠加在原始视图控制器的视图上并显示搜索结果。结果显示在由搜索显示控制器创建的表格视图中。除了原来的视图控制器,逻辑上还有其他四个角色。这些通常都由同一个对象播放,通常是原始视图控制器本身。

于 2011-11-30T23:06:18.750 回答
0

在我的情况下 UISearchDisplayController 使用正确的单元格类型(自定义)但单元格的高度是错误的,所以我不得不使用

(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 

修复它的方法。

于 2013-07-21T12:44:29.450 回答