2016-01-30 28 views
-1

您好我在我的應用程序(storyboard)中添加了一個UITableView自定義單元格。 現在我想以編程方式添加委託和數據源。如果在viewDidLoad中添加此代碼,則應用程序崩潰。如果我設置了tableview的代理和數據源,應用程序崩潰

tableView.delegate = self 
    tableView.dataSource = self 

錯誤:

2016-01-30 16:39:29.283 App[4068:880212] Unknown class TableCell in Interface Builder file. 
2016-01-30 16:39:29.289 App[4068:880212] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UITableViewCell 0x7fa3a356bb10> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key imgTeam1.' 
*** First throw call stack: 
(
0 CoreFoundation      0x0000000105302e65 __exceptionPreprocess + 165 
1 libobjc.A.dylib      0x0000000107042deb objc_exception_throw + 48 
2 CoreFoundation      0x0000000105302aa9 -[NSException raise] + 9 
3 Foundation       0x00000001056cb9bb -[NSObject(NSKeyValueCoding) setValue:forKey:] + 288 
4 UIKit        0x0000000105bd5fd9 -[UIView(CALayerDelegate) setValue:forKey:] + 173 
5 UIKit        0x0000000105edcf41 -[UIRuntimeOutletConnection connect] + 109 
6 CoreFoundation      0x00000001052434a0 -[NSArray makeObjectsPerformSelector:] + 224 
7 UIKit        0x0000000105edb924 -[UINib instantiateWithOwner:options:] + 1864 
8 UIKit        0x0000000105c654f7 -[UITableView _dequeueReusableViewOfType:withIdentifier:] + 428 
9 App         0x0000000105112cd9 _TFC6App14ViewController9tableViewfS0_FTCSo11UITableView21cellForRowAtIndexPathCSo11NSIndexPath_CSo15UITableViewCell + 233 
10 App         0x00000001051134ff _TToFC6App14ViewController9tableViewfS0_FTCSo11UITableView21cellForRowAtIndexPathCSo11NSIndexPath_CSo15UITableViewCell + 79 
11 UIKit        0x0000000105c78e43 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 766 
12 UIKit        0x0000000105c78f7b -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 74 
13 UIKit        0x0000000105c4da39 -[UITableView _updateVisibleCellsNow:isRecursive:] + 2996 
14 UIKit        0x0000000105c8201c -[UITableView _performWithCachedTraitCollection:] + 92 
15 UIKit        0x0000000105c68edc -[UITableView layoutSubviews] + 224 
16 UIKit        0x0000000105bd64a3 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 703 
17 QuartzCore       0x000000010a08859a -[CALayer layoutSublayers] + 146 
18 QuartzCore       0x000000010a07ce70 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366 
19 QuartzCore       0x000000010a07ccee _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24 
20 QuartzCore       0x000000010a071475 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277 
21 QuartzCore       0x000000010a09ec0a _ZN2CA11Transaction6commitEv + 486 
22 QuartzCore       0x000000010a09f37c _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92 
23 CoreFoundation      0x000000010522e367 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23 
24 CoreFoundation      0x000000010522e2d7 __CFRunLoopDoObservers + 391 
25 CoreFoundation      0x000000010522384c CFRunLoopRunSpecific + 524 
26 UIKit        0x0000000105b1a7cd -[UIApplication _run] + 402 
27 UIKit        0x0000000105b1f610 UIApplicationMain + 171 
28 App         0x000000010511567d main + 109 
29 libdyld.dylib      0x0000000107b4b92d start + 1 
) 
libc++abi.dylib: terminating with uncaught exception of type NSException 

我希望你知道如何解決這個問題。

+0

你會得到什麼錯誤? –

+0

我在我的問題中添加了錯誤。 – 123

+0

你應該對下面的答案給予反饋。這樣做將幫助我們發現一個可以幫助您的解決方案。如果你參與更多,那麼人們需要時間來幫助你。如果解決方案有幫助並投票,只需添加評論。如果解決方案無法解決問題,請分享有關此問題的更多詳細信息。參與得越多,問答環節就越適合您。 –

回答

0
  • 它不是故障數據源的&代表,檢查出口 imgTeam1連接correctlty。
  • StoryBoard定製電池的插座必須連接到自定義 細胞類屬性
  • 還有一點要補充的,而不是在 代碼在StoryBoard設置的設置數據源&代表。
+0

我刪除並重置故事板的所有插座到TableCell.swift。我無法在故事板上添加委託和數據源。不知道爲什麼。 TableView不能被選擇。 – 123

0

顯然你刪除了@IBOutlet weak var imgTeam1: UIImageView!(或重新命名了它),但仍然在接口生成器中連接。您需要再次添加該屬性或從視圖中刪除它。

0

你可能會刪除或錯過廈門國際銀行給出

0

的答案迄今是正確連接到委託imgTeam1。這個簡單地展示瞭如何採取糾正措施。您需要在故事板上爲發生錯誤的課程選擇橙色控制器。然後選擇連接檢查器。查找錯誤行中提到的關鍵:

2016-01-30 16:39:29.289 App[4068:880212] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UITableViewCell 0x7fa3a356bb10> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key imgTeam1.'

然後單擊下一步x在錯誤消息中提到的關鍵。這將清除錯誤。

如果您想確保您沒有任何有關TableView委託和數據存儲的更多錯誤消息,只需確認它們在連接檢查器上是否已經有連接。如果代理/數據存儲在故事板上已經有連接,則不需要在代碼中添加連接。

只是FYI,如果您使用UITableViewController,那麼您將自動設置委託/數據存儲和tableView連接,無需在代碼中添加任何內容。如果您使用帶有手動添加的tableView的UIViewController,則需要手動設置tableView,委託和數據存儲的連接。代理/數據存儲也可以使用故事板通過控制從連接檢查器拖動到橙色視圖控制器進行設置。

connections-inspector

相關問題