UITableView在创建单元格时抛出NSUnkownKeyException

时间:2016-07-08 23:30:45

标签: ios swift uitableview nsunknownkeyexception

我真的很难修复这个NSUnkownKeyException我现在正在UITableView中创建一个单元格。这是导致错误的代码行:

    let cell = tableView.dequeueReusableCellWithIdentifier("eventCell", forIndexPath: indexPath) as! EventTableViewCell

实际错误:

2016-07-08 19:08:58.879 myApp[15078:1230258] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<myApp.EventTableViewCell 0x7fbed3811000> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key view.'
*** First throw call stack:
(
0   CoreFoundation                      0x000000010c143d85 __exceptionPreprocess + 165
1   libobjc.A.dylib                     0x000000010df1fdeb objc_exception_throw + 48
2   CoreFoundation                      0x000000010c1439c9 -[NSException raise] + 9
3   Foundation                          0x000000010c51519b -[NSObject(NSKeyValueCoding) setValue:forKey:] + 288
4   UIKit                               0x000000010ca264b6 -[UIView(CALayerDelegate) setValue:forKey:] + 173
5   UIKit                               0x000000010cd357fb -[UIRuntimeOutletConnection connect] + 109
6   CoreFoundation                      0x000000010c07d890 -[NSArray makeObjectsPerformSelector:] + 224
7   UIKit                               0x000000010cd341de -[UINib instantiateWithOwner:options:] + 1864
8   UIKit                               0x000000010cab595f -[UITableView _dequeueReusableViewOfType:withIdentifier:] + 428
9   UIKit                               0x000000010cab5e32 -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:] + 71
10  myApp                     0x000000010a81735a _TFC15firebaseTestApp16CEViewController9tableViewfTCSo11UITableView21cellForRowAtIndexPathCSo11NSIndexPath_CSo15UITableViewCell + 330
11  myApp                     0x000000010a817ccf _TToFC15firebaseTestApp16CEViewController9tableViewfTCSo11UITableView21cellForRowAtIndexPathCSo11NSIndexPath_CSo15UITableViewCell + 79
12  UIKit                               0x000000010cac94f4 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 766
13  UIKit                               0x000000010cac962c -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 74
14  UIKit                               0x000000010ca9dd4f -[UITableView _updateVisibleCellsNow:isRecursive:] + 2996
15  UIKit                               0x000000010cad2686 -[UITableView _performWithCachedTraitCollection:] + 92
16  UIKit                               0x000000010cab9344 -[UITableView layoutSubviews] + 224
17  UIKit                               0x000000010ca26980 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 703
18  QuartzCore                          0x0000000111a60c00 -[CALayer layoutSublayers] + 146
19  QuartzCore                          0x0000000111a5508e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
20  QuartzCore                          0x0000000111a54f0c _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
21  QuartzCore                          0x0000000111a493c9 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277
22  QuartzCore                          0x0000000111a77086 _ZN2CA11Transaction6commitEv + 486
23  QuartzCore                          0x0000000111a777f8 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
24  CoreFoundation                      0x000000010c068c37 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
25  CoreFoundation                      0x000000010c068ba7 __CFRunLoopDoObservers + 391
26  CoreFoundation                      0x000000010c05e7fb __CFRunLoopRun + 1147
27  CoreFoundation                      0x000000010c05e0f8 CFRunLoopRunSpecific + 488
28  GraphicsServices                    0x000000010f218ad2 GSEventRunModal + 161
29  UIKit                               0x000000010c96bf09 UIApplicationMain + 171
30  myApp                     0x000000010a834bf2 main + 114
31  libdyld.dylib                       0x000000010e9fa92d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

我检查过的事情:

  1. 将TableView初始化为正确的类
  2. TableView连接到UITableViewController
  3. 我检查了所有可以找到的插座,以确保没有任何错误连接或空连接。
  4. 非常感谢您的帮助,非常感谢!

2 个答案:

答案 0 :(得分:1)

dequeueReusableCellWithIdentifier正在尝试从EventTableViewCell实例化.xib听起来该文件包含无效绑定,该绑定试图设置view属性(不存在)EventTableViewCell。在Xcode中查看你的xib,你可能会收到有关无效插座绑定的警告。

答案 1 :(得分:1)

你应该在nib / storyboard中选择tableview单元格并检查实用程序面板中的Outlets。如果有任何损坏的插座进入单元格,Xcode会将bang标记放到连接节点。要查找xib / storyboard,你应该使用EventTableViewCell搜索xcode