2

The background

I have an iPhone app that makes use of a UITableViewController sub class in several places by sub classing again for each use. One of those uses is a search controller.

@interface TableViewController : UITableViewController
// ...
@interface SearchTableViewController : TableViewController <UISearchDisplayDelegate, UISearchBarDelegate>

In the storyboard editor I have the same table view, cell structure, and reuse identifier in each view that makes use of TableViewController. Everywhere I am using it the storyboard is generating cells for me based on my design time prototypes so that in the tableView:cellForRowAtIndexPath: method I don't have to include the if (cell == nil) section.

The problem

I have done something wrong and my search controller's cells aren't being generated by the storyboard like the others. At first, I added in the if (cell == nil) bit to solve the problem but it causes my search to display blank rows. Actually, the search shows the correct number of blank rows. After you cancel the search, the results appear from out of the background. Here's the code from TableViewController:

- (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];
    }

    ModelObject* obj = [self.dataSource modelAtIndex:indexPath.row];
    UILabel* name = (UILabel*) [cell viewWithTag:1];
    name.text = obj.name;
    return cell;
}

I suspect there may be other details necessary to identify the issue but any tips on what kinds of things could cause this would be helpful. Thanks.

4

2 回答 2

1

如果您使用 Storyboard,请选择对象“带有搜索显示控制器的搜索栏”并将其放置在您的视图控制器中(比如说 VC)。然后 Xcode 会自动将搜索栏链接到 VC 中的搜索显示控制器。因此在 VC 中,您可以通过以下方式访问搜索显示控制器,self.searchDisplayController 并且您的 VC 应采用协议,例如UISearchBarDelegate, UISearchDisplayDelegate 您的搜索栏也可以通过以下方式访问self.searchDisplayController.searchBar

在 UISearchDisplayDelegate 协议中实现相关方法(表格视图等)。答对了!您的搜索结果将自动显示。

于 2012-12-23T09:17:31.480 回答
1

“tableview”的部分是错误的。

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

您应该改用 self.tableview (或链接到 tableview 的 IBOutlet 变量名)。像这样;

UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

当搜索完成时,cellForRowAtIndexPath 中的 tableview 参数将不是您在视图控制器中拥有的参数。这就是问题的原因。

于 2015-10-09T09:20:48.077 回答