当我在 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/