2014-02-28 51 views
0

我有一個帶數據的NSMutable數組。cellForRowAtIndexPath崩潰

我想在方法「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 

謝謝。

+1

NSString不會「識別」objectAtIndex。很可能是因爲你的變量'result'包含一個NSString,而不是NSArray。 –

回答

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] 
+0

如果我理解他們必須將字符串寫入數組的結果。但不幸的是,我仍然在做錯事。 if(indexPath.section == 0)NSDictionary * filtertNew = [self.nummeriertesArray objectAtIndex:0]; NSArray * result = [[filtertNew allKeys] objectAtIndex:1]; NSString * resultNew = [result objectAtIndex:indexPath.row]; cell.ergebnissLabel.text = resultNew; 有沒有人有想法。 – carlie

+0

@carlie - Greg向您解釋這條線是錯誤的:'NSArray * result = [[filtertNew allKeys] objectAtIndex:1];' –

+0

@HotLicks這段代碼從NSArray([filtertNew allKeys])中獲取第二個對象,它包含NSStrings結果是NSString不是NSArray。 – Greg

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:消息,自從實例沒有識別選擇器(即方法名稱),它拋出一個異常。

但是,您沒有包含任何有關您要做什麼的信息,因此我很難確切地告訴您需要做什麼才能正常工作。