cellForRowAtIndexPath崩溃

时间:2014-02-28 16:51:07

标签: iphone objective-c ios7

我有一个带数据的NSMutable数组。

我想在方法“cellForRowAtIndexPath”中填充各行。

表格视图一直在崩溃,我不知道为什么。

问题在于此行“cell.ergebnissLabel.text = [result objectAtIndex:indexPath.row]”。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *CellIdentifier = @"ErgebnisseCell";
ErgebnisseCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (indexPath.section == 0) {
NSDictionary*filtertNew = [self.nummeriertesArray objectAtIndex:0];
NSArray *result = [[filtertNew allKeys] objectAtIndex:1]
cell.ergebnissLabel.text = [result objectAtIndex:indexPath.row];
}

return cell;
}

错误消息如下所示。

2014-02-28 17:32:11.208 Info[2451:70b] -[__NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x10950c110
2014-02-28 17:32:11.210 Info[2451:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x10950c110'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000101caf295 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000101a0d99e objc_exception_throw + 43
    2   CoreFoundation                      0x0000000101d4045d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x0000000101ca0b8d ___forwarding___ + 973
    4   CoreFoundation                      0x0000000101ca0738 _CF_forwarding_prep_0 + 120
    5   Info                                0x000000010001f0da -[ErgebnisseTableViewController tableView:cellForRowAtIndexPath:] + 394
    6   UIKit                               0x000000010069574a -[UITableView _createPreparedCellForGlobalRow:withIndexPath:] + 348
    7   UIKit                               0x000000010067bb1b -[UITableView _updateVisibleCellsNow:] + 2337
    8   UIKit                               0x000000010068cfc1 -[UITableView layoutSubviews] + 207
    9   UIKit                               0x0000000100621943 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 354
    10  QuartzCore                          0x000000010021cdf2 -[CALayer layoutSublayers] + 151
    11  QuartzCore                          0x0000000100211959 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 363
    12  QuartzCore                          0x00000001002117da _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
    13  QuartzCore                          0x00000001001856e4 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 252
    14  QuartzCore                          0x000000010018675c _ZN2CA11Transaction6commitEv + 394
    15  UIKit                               0x00000001005c01f1 _UIApplicationHandleEventQueue + 10863
    16  CoreFoundation                      0x0000000101c3eb21 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    17  CoreFoundation                      0x0000000101c3e3f2 __CFRunLoopDoSources0 + 242
    18  CoreFoundation                      0x0000000101c5a26f __CFRunLoopRun + 767
    19  CoreFoundation                      0x0000000101c59b83 CFRunLoopRunSpecific + 467
    20  GraphicsServices                    0x0000000103587df5 GSEventRunModal + 161
    21  UIKit                               0x00000001005c2003 UIApplicationMain + 1010
    22  Info                                0x0000000100036143 main + 115
    23  libdyld.dylib                       0x00000001025a35fd start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

感谢。

2 个答案:

答案 0 :(得分:1)

这两行给出了一个错误:

NSArray *result = [[filtertNew allKeys] objectAtIndex:1]
[result objectAtIndex:indexPath.row];

// This gives you array of string
[filtertNew allKeys]
// This gives you string (second in array)
[[filtertNew allKeys] objectAtIndex:1]
//When you call that line you call 
[result objectAtIndex:indexPath.row]
//You call objectAtIndex: method on NSString object but there is not method like that on NSString

我相信你想这样做:

cell.ergebnissLabel.text = [[filtertNew allKeys] objectAtIndex:row]

答案 1 :(得分:0)

这是问题所在。首先,您的代码从数组中获取NSDictionary的实例:

NSDictionary *filtertNew = [self.nummeriertesArray objectAtIndex:0];

在下一行中,您将获得字典键的数组,这些键很可能是NSString的实例。然后,您可以访问数组中的第二个键,并将其存储在名为result的变量中。

NSArray *result = [[filtertNew allKeys] objectAtIndex:1]

不幸的是,尽管result变量的类型为NSArray *,但您指定的值是字符串,而不是数组。接下来,您将objectAtIndex:发送到该字符串,这会导致应用崩溃,因为NSString没有objectAtIndex:方法。

cell.ergebnissLabel.text = [result objectAtIndex:indexPath.row];

这就是您在控制台上看到以下错误消息的原因:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', 
reason: '-[__NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x10950c110'

这告诉您NSString的实例发送了objectAtIndex:消息,并且由于实例无法识别选择器(即方法名称),因此它引发了异常。< / p>

但是,你没有提供任何关于你正在尝试做什么的信息,所以我很难准确告诉你为了让它正常工作你需要做些什么。