完全按照教程,UITableView无法正常工作

时间:2015-11-05 18:55:55

标签: xcode swift uitableview

我正在尝试学习如何使用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合作[17405:218899] *终止   应用程序由于未捕获的异常' NSUnknownKeyException',原因:   ' [   setValue:forUndefinedKey:]:此类不是键值   符合编码的myTableView密钥。'   * 第一次抛出调用堆栈:(0 CoreFoundation 0x000000010ac16f45 exceptionPreprocess + 165 1 libobjc.A.dylib
  0x000000010c93adeb objc_exception_throw + 48 2 CoreFoundation
  0x000000010ac16b89 - [NSException raise] + 9 3基础
  0x000000010afdfa6b - [NSObject(NSKeyValueCoding)setValue:forKey:] +   288 4 UIKit 0x000000010b5be04c    - [UIViewController setValue:forKey:] + 88 5 UIKit 0x000000010b7eba71 - [UIRuntimeOutletConnection connect] + 109 6
  CoreFoundation 0x000000010ab57a80 - [NSArray   makeObjectsPerformSelector:] + 224 7 UIKit
  0x000000010b7ea454 - [UINib instantiateWithOwner:options:] + 1864 8
  UIKit 0x000000010b5c4c16    - [UIViewController _loadViewFromNibNamed:bundle:] + 381 9 UIKit 0x000000010b5c5542 - [UIViewController loadView] + 178 10 UIKit
  0x000000010b5c58a0 - [UIViewController loadViewIfRequired] + 138 11   UIKit 0x000000010b5c6013    - [UIViewController视图] + 27 12 UIKit 0x000000010bd687e7 - [_ UIFullscreenPresentationController   _setPresentedViewController:] + 87 13 UIKit 0x000000010b595dde - [UIPresentationController   initWithPresentedViewController:presentsViewController:] + 133 14   UIKit 0x000000010b5d89ba    - [UIViewController _presentViewController:withAnimationController:completion:] + 4004 15 UIKit 0x000000010b5dbc5c    - [UIViewController _performCoordinatedPresentOrDismiss:animated:] + 489 16 UIKit 0x000000010b5db76b    - [UIViewController presentViewController:animated:completion:] + 179 17 UIKit 0x000000010b5e0e0f    - [UIViewController _showViewController:withAction:sender:] + 280 18 UIKit 0x000000010ba2b130   __66- [UIStoryboardShowSegueTemplate newDefaultPerformHandlerForSegue:] _ block_invoke + 147 19 UIKit
  0x000000010bb527c4 - [UIStoryboardSegueTemplate   _performWithDestinationViewController:sender:] + 460 20 UIKit 0x000000010bb525c7 - [UIStoryboardSegueTemplate _perform:] + 82 21   UIKit 0x000000010bb5288b    - [UIStoryboardSegueTemplate执行:] + 156 22 UIKit 0x000000010b434e91 - [UIApplication sendAction:to:from:forEvent:] + 92     23 UIKit 0x000000010b5a04d8    - [UIControl sendAction:to:forEvent:] + 67 24 UIKit 0x000000010b5a07a4 - [UIControl _sendActionsForEvents:withEvent:] + 311     25 UIKit 0x000000010b59f8d4    - [UIControl touchesEnded:withEvent:] + 601 26 UIKit 0x000000010b4a2ed1 - [UIWindow _sendTouchesForEvent:] + 835 27 UIKit   0x000000010b4a3c06 - [UIWindow sendEvent:] + 865 28 UIKit
  0x000000010b4532fa - [UIApplication sendEvent:] + 263 29 UIKit
  0x000000010b42dabf _UIApplicationHandleEventQueue + 6844 30   CoreFoundation 0x000000010ab43011   __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION
+ 17 31 CoreFoundation 0x000000010ab38f3c   __CFRunLoopDoSources0 + 556 32 CoreFoundation 0x000000010ab383f3 __CFRunLoopRun + 867 33 CoreFoundation
  0x000000010ab37e08 CFRunLoopRunSpecific + 488 34 GraphicsServices
  0x000000010f20aad2 GSEventRunModal + 161 35 UIKit
  0x000000010b43330d UIApplicationMain + 171 36合作
  0x000000010aa22f6d main + 109 37 libdyld.dylib
  0x000000010d44292d开始+ 1 38 ???   0x0000000000000001 0x0 + 1)libc ++ abi.dylib:终止于   NSException(lldb)

类型的未捕获异常

由于

2 个答案:

答案 0 :(得分:1)

转到故事板,右键单击您的tableView并检查它的插座是否连接到名称" myTableView"或不。如果是,请使用十字图标将其断开连接。我猜你的tableView有两个出口,一个名为" myTableView"和其他" tableView"。确保将名称保留为代码中的名称。

答案 1 :(得分:0)

第一件事:

Connect the IBOutlet to the actual UITableView Object (CTRL + Drag)

然后在viewDidLoad()

中将TableView Delegate连接到self
tableView.delegate = self

并确保您提供的单元格名称与 homeCell

相同