11

所以我有一个包含部分和行的 tableView,它使用自定义单元格类。自定义单元格有一个图像视图和一些标签。表格视图工作正常,搜索工作正常,除了搜索不显示我的自定义单元格类中的任何标签,只显示具有正确图像的 imageView。我很困惑为什么会这样,特别是因为图像仍然显示,而不是标签。这是一些代码。

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


//TODO: problem with search view controller not displaying labels for the cell, needs fixing
JSBookCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

if(cell == nil) {
    cell = [[JSBookCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

JSBook *book = nil;
//uses the appropriate array to pull the data from if a search has been performed
if(tableView == self.searchDisplayController.searchResultsTableView) {
    book = self.filteredTableData[(NSUInteger)indexPath.section][(NSUInteger)indexPath.row];
}
else {
    book = self.books[(NSUInteger)indexPath.section][(NSUInteger)indexPath.row];
}

FFMetaData *data = [self.ff metaDataForObj:book];
cell.titleLabel.text = book.title;
cell.priceLabel.text = [NSString stringWithFormat:@"$%@", book.price];
cell.authorLabel.text = book.author;
cell.descriptionLabel.text = book.description;

cell.dateLabel.text = [self.formatter stringFromDate:data.createdAt];
if(book.thumbnail == nil) {
    cell.imageView.image = [UIImage imageNamed:@"messages.png"];
    [self setCellImage:cell withBook:book atIndex:indexPath withTableView:tableView];
}
else {
    cell.imageView.image = [UIImage imageWithData:book.thumbnail];
}

return cell;

}

在这个问题之前,我在 tableView 中只有一个部分,并且一切正常。现在我有多个部分和行,正如我所描述的那样,搜索被破坏了。有任何想法吗?另外,因为[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];我曾经有[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];但现在如果我使用它,当我尝试搜索时会遇到一个奇怪的异常:

NSInternalInconsistencyException',原因:'在无效索引路径(2 个索引 [1, 1])处请求矩形'

所以这也让我感到困惑。谢谢您的帮助!

4

6 回答 6

30
[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

不起作用,因为表格视图单元格已注册到特定的表格视图。这不适用于您的搜索结果控制器表视图。您确实自己发现了这一点并切换到:

[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

这是正确的做法。

此外,在情节提要中设计您的自定义单元格对于您的搜索结果控制器将不起作用,因为您无法为搜索表格视图设计单元格,只能用于主表格视图。

是的,您可以像在此处一样为您的搜索表视图注册该类,

[self.searchDisplayController.searchResultsTableView registerClass:[JSBookCell class] forCellReuseIdentifier:CellIdentifier];

但这不会有您在情节提要的自定义单元格中设计的任何东西。您必须以编程方式创建所有内容。

于 2013-04-09T06:42:15.020 回答
24

当我尝试搜索以及如何解决它时,我遇到了同样的异常。

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    }else{
       cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];     
    }
于 2013-09-11T14:00:16.810 回答
9

我对带有搜索栏和搜索显示的 UITableView 的总结,使用故事板原型中设计的相同自定义单元格:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CellIdentifierAsYouDefinedInStoryboard";

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

    if (cell == nil) {
        cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        /* searchResultsTableView code here */
    } else {
        /* Base tableView table code here */
    }

    /* more cell code here */

    return cell;
}


然后为 searchResultsTableView 添加此行以匹配您的自定义单元格高度:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.searchDisplayController.searchResultsTableView setRowHeight:self.tableView.rowHeight];

     /* more of your viewDidLoad code */
}
于 2013-12-20T04:41:49.900 回答
6

如果您在 UIViewController 中使用 UITableView 并且想要重用您在 StoryBoard 中为您的 searchDisplayController 创建的单元格标识符,请尝试以下操作:

StoryBoard > UIViewController > Reference Outlets > 将 tableView 链接到 UIViewController 的 .h 文件,并将其命名为 tableView 之类的东西,所以你应该有这样的东西:

@property (strong, nonatomic) IBOutlet UITableView *tableView;

所以不要这样做:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]
}

做这个

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]
}
于 2013-10-21T10:46:38.180 回答
0

如果您在情节提要中创建一个单元格,则不应注册该类(这实际上搞砸了)。如果您在 code 中制作单元格,则您注册类,如果您在 nib 中制作单元格,则注册一个 nib。如果您在情节提要中完成,则无需注册任何内容,您使用 dequeueReusableCellWithIdentifier:forIndexPath:,并且您根本不需要 if (cell == nil) 子句。

于 2013-04-09T04:44:01.890 回答
0

试试这个,它对我有用

JSBookCell * cell = [yourtableview dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
于 2015-03-11T11:38:02.137 回答