在XCode 4.2中使用动态自定义UITableViewCell,使用Storyboards和UISeachDisplayController

Reu*_*ven 14 iphone uitableview ios xcode-storyboard

我已经使用Xcode 4.2来创建基于故事板的iOS应用程序.我的一个屏幕包含一个UITableViewController,使用动态自定义单元格.

到现在为止还挺好.

现在,我想添加一个UISearchDisplayController以允许过滤我的列表.

出于某种原因,UISearchDisplayController将不会显示我的自定义单元格,我找不到强制它的方法...

这是我的cellForRowAtIndexPath方法的样子:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"QueueListCell";
    QueueListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[QueueListTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                             reuseIdentifier:CellIdentifier];
    }
    assert(cell);

    if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]) {
        indexPath = [_indexPathsForSearchResults objectAtIndex:indexPath.row];
    }

    // Set up the cell...
    NSDictionary* itemDict = [_ListItems objectAtIndex:indexPath.row];

    cell.labelQueueName.text = [itemDict objectForKey:kQueueName];
    cell.labelQueueNumItems.text = [[itemDict objectForKey:kQueueNumItems] stringValue];

    return cell;    
}
Run Code Online (Sandbox Code Playgroud)

有关如何使这个工作的任何想法?我的意思是,我的UISearchDisplayController表显示正确的结果数量(我知道,因为我可以点击它们,我添加了一个NSLog让我知道我点击了什么......)

这是我的表格视图 这是我的表格视图

这就是搜索显示表的样子...... 这就是搜索显示表的样子......

我的问题/问题是如何使UISearchDisplayController表视图显示我的自定义单元格?

任何帮助赞赏...

鲁文

Sag*_*ari 27

特定于查询的答案

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
}
Run Code Online (Sandbox Code Playgroud)

有关完整示例,请获取Apple网站示例代码.

一步一步的插图

  • 最初,我尝试从方法参数tableView中取出一个单元格(对我的自定义单元格一无所知)...使用@Spark的答案,我改变了我的代码,将单元格_always_从self.tableView出列 - 表格_knows_关于我的定制细胞...... (4认同)
  • 星火,你是我的英雄!:-)所以,太令人沮丧了.然而,修复如此简单和优雅!谢谢! (2认同)