ios - 应用程序在执行表委托(delegate)方法时崩溃

标签 ios objective-c uitableview

当我在 aepub 阅读器中运行搜索功能时,我的应用程序崩溃了。它在 index 方法中进入 cellfor 行,当它执行 NSLOg(@"%@",hit.neighbourText) 时显示异常。

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

        UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        }

        cell.textLabel.adjustsFontSizeToFitWidth = YES;

        NSLog(@"indexpath%d",indexPath.row);
        NSLog(@"%@",[results objectAtIndex:[indexPath row]]);

        hit = (SearchResult*)[results objectAtIndex:[indexPath row]];

        if([results count]>0) {
            NSLog(@"%@",hit.neighboringText);
            cell.textLabel.text = [NSString stringWithFormat:@"...%@...", hit.neighboringText];
            cell.detailTextLabel.text = [NSString stringWithFormat:@"Chapter %d - page %d", hit.chapterIndex, hit.pageIndex+1];

           return cell;
        }
    }

我正在为 hit.neighboringText 获取一些值,但在那之后,我重新加载我的 tableview 然后将引发以下异常,为什么?

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString neighboringText]: unrecognized selector sent to instance 0x1481c4' *** First throw call stack:

最佳答案

这是因为 hit 实际上是一个 NSString 对象,而不是你期望的 SearchResult 对象:

hit = (SearchResult*)[results objectAtIndex:[indexPath row]];

线索在异常文本中:

-[__NSCFConstantString neighboringText]: unrecognized selector sent to instance ...
  ^^^^^^^^^^^^^^^^^^^^                   ^^^^^^^^^^^^^^^^^^^^^

SearchResult 进行再多的转换也不会改变这一点。

编辑:实际上,无论您在何处看到类型转换,您都应该怀疑您正在处理的实际对象。如果您不确定,请使用 isKindOfClass: 检查它。

关于ios - 应用程序在执行表委托(delegate)方法时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16602953/

相关文章:

objective-c - 如何在 XCode 中将自定义 UIButton 添加到 nib 文件?

ios - 无需任何应用交互(例如打开应用或推送通知)即可重置或清除 iOS 应用角标(Badge)

ios - 重新显示 View 时,另一个表的单元格中的动态大小表不会调整大小

ios - 在 UIViewController 中使用自定义 UITableView

ios - UIImageview 不会在 super View 中居中

ios - Swift:Objective-C 方法冲突(尝试更改 calloutview 时出错)

ios - UIImageView没有调整大小

ios - 基于 pod 的 Swift 通用框架

iphone - 为什么在 dismissViewControllerAnimated 之后父框架没有保持相同的大小?

ios - 将 UITableView 添加为 SubView 时发送到实例的无法识别的选择器