我正在尝试学习如何使用UITableView元素,然而,尽管观看了一个简单的教程并复制了一个字符一个字符的代码,表视图仍然会崩溃应用程序,尽管编辑器中没有出现任何错误。
代码:
import UIKit
class viewFriendsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var dataSource: [String] = []
var items = ["one", "two"]
override func viewDidLoad() {
super.viewDidLoad()
dataSource = ["new orleans", "San Fransisco", "Seattle", "New York", "London"]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataSource.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("homeCell", forIndexPath: indexPath)
cell.textLabel?.text = dataSource[indexPath.row]
return cell
}
}
错误:
2015-11-05 13:51:49.120协作[1745:218899]*由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[setValue:forUndefinedKey:]:该类不符合密钥myTableView的键值编码。”*第一次抛出调用堆栈:(0 CoreFoundation 0x000000010ac16f45 exceptionPreprocess 165 1 libobjc.A.dylib
0x000000010c93adeb objc_exception_othrow 48 2 CoreFoundation
x00000010ac18b89-[NSE exception raise]9 3 Foundation
0x000000010afdfa6b-[NSObject(NSKeyValueCoding)setValue:forKey:]288 4 UIKit 0x000000010b5be04c-[UIIViewController setValue:for Key:]88 5 UIKit0x000000010b7eba71-[UILruntimeOutletConnection connect]109 6
CoreFoundation 0x000000010ab57a80-[NArray makeObjectsPerformalSelector]224 7 UIKit
0x000000010b17ea454-[UILib instantiateWithOwner:选项:]1864 8
UIKit 0x000000010b5c4c16-[UILViewController _loadViewFromNibNamed:捆绑包:]381 9 UIKit 0x0000000 10b5c55542-[UILviewController loadView]178 10 UIKit
0x000000010b5c58a0-[UIViewController loadViewIfRequired]138 11 UIKit 0x000000010b5c6013-[UIVViewController视图]27 12 UIKit 0x0000000 10bd687e7-[UIFullscreenPresentationController_setPresentedViewController:]87 13 UIKit 0x0000000010b595dde-[UIIPresentationControl initWithPresentedViewController:presentingViewController:133 14 UIKit 0x00000010b5d89ba-[UIViewController_presentViewController:带动画控制器:完成:]4004 15 UIKit 0x000000010b5dbc5c-[UIViewController_performCoordinatedPresentOrDismiss:动画:]489 16 UIKit 0x0000000 10b5db76b-[UIVViewControllerpresentViewController:动画:完成:]179 17 UIKit 0x0000000010b5e0e0f-[UIVviewController_showViewController:withAction:sender:]280 18 UIKit0x000000010ba2b130 __66-[UIStoryboardShowSegueTemplate newDefaultPerformHandlerForSegue:]_block_invoke 147 19 UIKit
0x000000010bb527c4-[UIStorayboardSegueTemplate performWithDestinationViewController:sender:]460 20 UIKit 0x000000010b525c7-[UISoryboardSequeTemplate perform:]82 21 UIKit 0x0000000 10bb5288b-[UIStor yboardSegue Template perform:]156 22 UIKit 0x0000000010b434e91-[UIApplication sendAction:to:from:forEvent:]92 23 UIKit 0x000000010b5a04d8-[UIControl sendAction:to:forEvent:]67 24 UIKit 0x0000000 10b5a07a4-[UICntrol _sendActionsForEvents:withEvent:]311 25 UIKit 0x0000000010b59f8d4-[UIControl touchesEnded:withEvent:]601 26 UIKit 0x00000010b4a2ed1-[UICWindow _sendTouchesForEvent:]835 27 UIKit 0x000000 10b4a3c06-[UICwindow sendEvent:]865 28UIKit
0x000000010b4532fa-[UIApplication sendEvent:]263 29 UIKit
0x00000000b42dabf_UIApplicationHandleEventQueue 6844 30 CoreFoundation 0x000000010ab43011 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION 17 31 CoreFoundation 0x00000010ab38f3c __CFRunLoopDoSources 0 556 32 CoreFoundation x00000010ab83f3 __CFRunLoopRun 867 33 CoreFoundation
CFRunLoopRunSpecific 488 34 GraphicsServices
0x000000010f20ad2 GSEventRunModal 161 35 UIKit
x00000010b43330d UIApplicationMain 171 36协作
0x000000010aa22f6d main 109 37 libdyld.dylib
0x00000010d44292d启动1 38
0x0000000000000001 0x0 1)libc abi.dylib:以NSException(lldb)类型的未捕获异常终止
谢谢你。
转到故事板,右键单击您的tableView并检查它的出口是否连接到名称“myTableView”。如果是,请使用十字图标断开它。我猜您的tableView有两个出口,一个名为“myTableView”,另一个名为“tableView”。确保您在代码中保留名称。
第一件事:
Connect the IBOutlet to the actual UITableView Object (CTRL + Drag)
然后在viewDidLoad()中将TableView Delegate连接到自身
tableView.delegate = self
并且还要确保您提供的单元格名称与homeCell相同