0

我正在尝试创建一个表格视图,将许多推文加载到表格中(每个单元格一个等)。

我正在使用 NSXMLParser 来获取信息,并且已经使用我想要的推文选择创建了一个数组。

但是,当我尝试将它们添加到表格单元格时,应用程序会在线崩溃:

cell.textLabel.text = cellValue;

在此之前的 NSLog 在控制台中显示该应用程序正在获取正确的数据,所以我对为什么这不起作用感到有些困惑。

这是似乎有问题的代码块:

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

 static NSString *CellIdentifier = @"Cell";

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

 // Set up the cell...
 NSString *cellValue = [statuses objectAtIndex:indexPath.row];
 NSLog(@"%@", cellValue);


 cell.textLabel.text = cellValue;

 return cell;}

如果它有所作为,我使用的是 ARC 和最新版本的 XCode。

我对这一切还是很陌生,所以如果我需要提供一些额外的信息,请告诉我。

提前致谢。

编辑:

回溯给出以下内容:

* thread #1: tid = 0x2003, 0x918a19c6 libsystem_kernel.dylib`__pthread_kill + 10, stop reason = signal SIGABRT
frame #0: 0x918a19c6 libsystem_kernel.dylib`__pthread_kill + 10
frame #1: 0x9968ff78 libsystem_c.dylib`pthread_kill + 106
frame #2: 0x99680bdd libsystem_c.dylib`abort + 167
frame #3: 0x03c93e78 libc++abi.dylib`_Unwind_DeleteException
frame #4: 0x03c9189e libc++abi.dylib`_ZL17default_terminatev + 34
frame #5: 0x0154df4b libobjc.A.dylib`_objc_terminate + 94
frame #6: 0x03c918de libc++abi.dylib`_ZL19safe_handler_callerPFvvE + 13
frame #7: 0x03c91946 libc++abi.dylib`std::terminate() + 23
frame #8: 0x03c92ab2 libc++abi.dylib`__cxa_throw + 110
frame #9: 0x0154de15 libobjc.A.dylib`objc_exception_throw + 311
frame #10: 0x013bdced CoreFoundation`-[NSObject doesNotRecognizeSelector:] + 253
frame #11: 0x01322f00 CoreFoundation`___forwarding___ + 432
frame #12: 0x01322ce2 CoreFoundation`_CF_forwarding_prep_0 + 50
frame #13: 0x0015168f UIKit`-[UILabel setText:] + 56
frame #14: 0x00003088 Twitter`-[TwitterViewController tableView:cellForRowAtIndexPath:] + 376 at TwitterViewController.m:131
frame #15: 0x000ace0f UIKit`-[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] + 494
frame #16: 0x000ad589 UIKit`-[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:] + 69
frame #17: 0x00098dfd UIKit`-[UITableView(_UITableViewPrivate) _updateVisibleCellsNow:] + 1350
frame #18: 0x000a7851 UIKit`-[UITableView layoutSubviews] + 242
frame #19: 0x00052301 UIKit`-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 145
frame #20: 0x013bde72 CoreFoundation`-[NSObject performSelector:withObject:] + 66
frame #21: 0x01d6692d QuartzCore`-[CALayer layoutSublayers] + 266
frame #22: 0x01d70827 QuartzCore`CA::Layer::layout_if_needed(CA::Transaction*) + 231
frame #23: 0x01cf6fa7 QuartzCore`CA::Context::commit_transaction(CA::Transaction*) + 377
frame #24: 0x01cf8ea6 QuartzCore`CA::Transaction::commit() + 374
frame #25: 0x01d8430c QuartzCore`+[CATransaction flush] + 52
frame #26: 0x000124c6 UIKit`-[UIApplication _reportAppLaunchFinished] + 39
frame #27: 0x00012bd6 UIKit`-[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 1324
frame #28: 0x00021743 UIKit`-[UIApplication handleEvent:withNewEvent:] + 1027
frame #29: 0x000221f8 UIKit`-[UIApplication sendEvent:] + 68
frame #30: 0x00015aa9 UIKit`_UIApplicationHandleEvent + 8196
frame #31: 0x012a6fa9 GraphicsServices`PurpleEventCallback + 1274
frame #32: 0x013901c5 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
frame #33: 0x012f5022 CoreFoundation`__CFRunLoopDoSource1 + 146
frame #34: 0x012f390a CoreFoundation`__CFRunLoopRun + 2218
frame #35: 0x012f2db4 CoreFoundation`CFRunLoopRunSpecific + 212
frame #36: 0x012f2ccb CoreFoundation`CFRunLoopRunInMode + 123
frame #37: 0x000122a7 UIKit`-[UIApplication _run] + 576
frame #38: 0x00013a9b UIKit`UIApplicationMain + 1175
frame #39: 0x0000239d Twitter`main + 141 at main.m:16
frame #40: 0x00002305 Twitter`start + 53

调试控制台显示:

2012-04-08 10:10:05.084 Twitter[25309:f803] (
    {
    text = "Have you shared the Shakedown yet? http://t.co/WHrIC9w7";
},
    {
    text = "For all you closet rocknrollas pencil in Sat 12th May The Rebirth of Rock n Roll Party. Haywire Saint @ The Good... http://t.co/OXHKlLIV";
},
    {
    text = "4 weeks today: Vocal tracks will be getting recorded at The Premises Studios";
},
    {
    text = "Rehearsal tonight in preparation to some big recording next month!";
},
    {
    text = "haywire saint 'great taste.'  Tune. \n\nhttp://t.co/GKmu5Lna http://t.co/0fii55Hw";
},
    {
    text = "Meeting up with an old roadie for The Cure today. oh the stories...... http://t.co/UeUYccme";
},
    {
    text = "Satisfying day of programming today.. Haywire Saint app coming along nicely with the custom music player ready to rock 'n' roll!";
},
    {
    text = "Happy Friday Everyone!";
},
    {
    text = "We had a great time at The Premises Studios yesterday. We'll be back there before long :D x";
},
    {
    text = "I posted a new photo to Facebook http://t.co/73qAnCvk";
}
)

2012-04-08 10:10:05.093 Twitter[25309:f803] {
text = "Have you shared the Shakedown yet? http://t.co/WHrIC9w7";
}

2012-04-08 10:10:05.094 Twitter[25309:f803] -[__NSCFDictionary isEqualToString:]:      unrecognized selector sent to instance 0x6877a50
2012-04-08 10:10:05.096 Twitter[25309:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary isEqualToString:]: unrecognized selector sent to instance 0x6877a50'
*** First throw call stack:
(0x13bc052 0x154dd0a 0x13bdced 0x1322f00 0x1322ce2 0x15168f 0x3088 0xace0f 0xad589 0x98dfd 0xa7851 0x52301 0x13bde72 0x1d6692d 0x1d70827 0x1cf6fa7 0x1cf8ea6 0x1d8430c 0x124c6 0x12bd6 0x21743 0x221f8 0x15aa9 0x12a6fa9 0x13901c5 0x12f5022 0x12f390a 0x12f2db4 0x12f2ccb 0x122a7 0x13a9b 0x239d 0x2305)
terminate called throwing an exception2012-04-08 10:10:05.924 Twitter[25309:f803] -[__NSCFConstantString count]: unrecognized selector sent to instance 0x5b30
4

2 回答 2

1

cell.textLabel.text需要一个NSString *,所以在这一行之前:

cell.textLabel.text = cellValue;

尝试 :

NSLog(@"%@", [cellValue class]);

或者

NSLog(@"%i", [cellValue isKindOfClass:[NSString class]]); 

找出确切的对象类型。

于 2012-04-08T17:26:12.627 回答
1

statuses数组似乎包含字典,而不是字符串。每个字典都有一个键,“文本”。

因此,您可以通过更改以下行来修复崩溃:

NSString *cellValue = [statuses objectAtIndex:indexPath.row];

NSString *cellValue = [[statuses objectAtIndex:indexPath.row] objectForKey:@"text"];

或者,如果您要以statuses不同的方式构造数组,您可以使其直接包含字符串,而无需不必要地包含在字典中。如果不知道您是如何构建的,很难告诉您如何修复该部分。

于 2012-04-08T12:54:31.537 回答