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

时间:2011-12-07 12:17:11

标签: iphone ios uitableview 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;    
}

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

这是我的表格视图 This is my table view

这是搜索显示表的样子...... This is how the search display table looks like...

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

任何帮助表示赞赏...

鲁文

1 个答案:

答案 0 :(得分:27)

特定于查询的答案

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
}

完整示例,请sample code from apple's site

Step by step illustration