【问题标题】:The Application crashes at replaceobjectatindex应用程序在 replaceobjectatindex 处崩溃
【发布时间】:2015-02-03 07:47:28
【问题描述】:

应用程序在 replaceobjectatindex 处崩溃。 clickedbuttonpath.row 返回 nil 值。需要帮助。

- (void)textFieldDidEndEditing:(UITextField *)textField 
{ 
if (textField.text.length>0)
 { 
UITableViewCell *clickedCell = (UITableViewCell *)[[textField superview] superview];
 NSIndexPath *clickedButtonPath = [addSizesTableView_ indexPathForCell:clickedCell]; 
if (textField.tag==6) 
{ 
[textDic setObject:textField.text forKey:clickedButtonPath]; 
[orderArr replaceObjectAtIndex:clickedButtonPath.row withObject:textField.text];

这是正在获取的崩溃日志。

'NSInvalidArgumentException',原因:'* setObjectForKey: key cannot be nil' * 首先抛出调用栈: ( 0 CoreFoundation 0x0000000106d1ef35 异常预处理 + 165 1 libobjc.A.dylib 0x00000001062acbb7 objc_exception_throw + 45 2 核心基础 0x0000000106c25998 -[__NSDictionaryM setObject:forKey:] + 968 3 BookMyStock 0x0000000103559ac0 -[AddSizesOfColors textFieldDidEndEditing:] + 368 4 UIKit 0x0000000105289549-[UITextField_resignFirstResponder] + 382 5 UIKit 0x0000000104ccc4f5-[UIResponder resignFirstResponder] + 236 6 UIKit 0x00000001052892d4-[UITextField resignFirstResponder] + 114 7 UIKit 0x0000000105295953-[UIView(UITextField) endEditing:] + 173 8 BookMyStock 0x0000000103557c2d -[AddSizesOfColors addTFTotheView] + 285 9 UIKit 0x0000000104b568be-[UIApplication sendAction:to:from:forEvent:] + 75 10 UIKit 0x0000000104c5d410-[UIControl_sendActionsForEvents:withEvent:] + 467 11 UIKit 0x0000000104c5c7df-[UIControl touchesEnded:withEvent:] + 522 12 UIKit 0x0000000104b9c308-[UIWindow_sendTouchesForEvent:] + 735 13 UIKit 0x0000000104b9cc33 -[UIWindow 发送事件:] + 683 14 UIKit 0x0000000104b699b1-[UIApplication sendEvent:] + 246 15 UIKit 0x0000000104b76a7d _UIApplicationHandleEventFromQueueEvent + 17370 16 UIKit 0x0000000104b52103 _UIApplicationHandleEventQueue + 1961 17 核心基础 0x0000000106c54551 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17 18 核心基础 0x0000000106c4a41d __CFRunLoopDoSources0 + 269 19 核心基础 0x0000000106c49a54 __CFRunLoopRun + 868 20 核心基础 0x0000000106c49486 CFRunLoopRunSpecific + 470 21 图形服务 0x0000000108bdc9f0 GSEventRunModal + 161 22 UIKit 0x0000000104b55420 UIApplicationMain + 1282 23 BookMyStock 0x0000000103492913 主要 + 115 24 libdyld.dylib 0x0000000107c7e145 开始 + 1 ) libc++abi.dylib:以 NSException 类型的未捕获异常终止

【问题讨论】:

  • 你检查了你得到的索引
  • 请发布崩溃日志,否则没有人可以帮助您:)
  • 欢迎来到 Stack Overflow。请参阅帮助中心,了解如何提出一个很有可能得到回答的问题 (stackoverflow.com/help/how-to-ask)。具体来说,包括可以更好地理解问题的错误消息。
  • 此代码在 ios 7 上运行良好,但在 ios 8 中崩溃。

标签: ios iphone uitableview nsindexpath


【解决方案1】:

[[textField superview] superview] 在 iOS 8 中返回 nil,而在 iOS 7 中返回一些值。正如以下链接中所建议的,不要依赖视图层次结构。

您可以查看此链接Getting UITableViewCell from its superview iOS 7 issue

你可以试试下面这样的结构

CGPoint subviewPosition = [textField convertPoint:CGPointZero toView:self.tableView];
NSIndexPath* clickedButtonPath = [addSizesTableView_ indexPathForRowAtPoint:subviewPosition];

【讨论】:

  • 感谢 Vinoth。我试过你的代码,但问题是应用程序在 [orderArr replaceObjectAtIndex:clickedButtonPath.row withObject:textField.text];
  • 你能保留断点或 NSLog 并查看 clickedCell 在 iOS 8 中获得非 nil 值吗?因为如果clickedCell为nil,那么clickedButtonPath就是nil,那么clickedButtonPath.row就是nil。
  • UITableViewCell *clickedCell = (UITableViewCell *)[[textField superview] superview];
  • clickedCell 没有给出 nil 值。
猜你喜欢
  • 2013-09-28
  • 2014-11-02
  • 2011-09-01
  • 2020-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多