首页 文章

如何修复错误:此类不是键值tableView的键值编码兼容 . [重复]

提问于
浏览
32

这个问题在这里已有答案:

我用 Table ViewSegmented Control 创建了一个应用程序,这是我第一次 . 我'm using some code and some tutorials, but It'不能正常工作 . 当我运行我的应用程序时's crashing and it' s在日志中显示此错误:

MyApplication [4928:336085] *由于未捕获的异常'NSUnknownKeyException'而终止应用程序,原因:'[setValue:forUndefinedKey:]:此类不是密钥tableView的密钥值编码兼容 . *第一次抛出调用堆栈:(0 CoreFoundation 0x000000010516fd85 __exceptionPreprocess 165 1 libobjc.A.dylib 0x0000000105504deb objc_exception_throw 48 2 CoreFoundation 0x000000010516f9c9 - [NSException raise] 9 3 Foundation 0x000000010364e19b - [NSObject(NSKeyValueCoding)setValue:forKey:] 288 4 UIKit 0x0000000103c37d0c - [ UIViewController setValue:forKey:] 88 5 UIKit 0x0000000103e6e7fb - [UIRuntimeOutletConnection connect] 109 6 CoreFoundation 0x00000001050a9890 - [NSArray makeObjectsPerformSelector:] 224 7 UIKit 0x0000000103e6d1de - [UINib instantiateWithOwner:options:] 1864 8 UIKit 0x0000000103c3e8d6 - [UIViewController _loadViewFromNibNamed:bundle:] 381 9 UIKit 0x0000000103c3f202 - [UIViewController loadView] 178 10 UIKit 0x0000000103c3f560 - [UIViewController loadViewIfRequired] 138 11 UIKit 0x0000000103c3fcd3 - [UIViewController视图] 27 12 UIKit 0x000000010440b024 - [_ UIFullscreenPresentationController _setPresentedViewController:] 87 13 UIKit 0x0000000103c0f5ca - [UIPresentationController initWithPresentedViewController:presentingViewController:] 133 14 UIKit 0x0000000103c525bb - [UIViewController _presentViewController:withAnimationController:completion:] 4002 15 UIKit 0x0000000103c5585c - [UIViewController _performCoordinatedPresentOrDismiss:animated:] 489 16 UIKit 0x0000000103c5536b - [UIViewController presentViewController:animated:completion:] 179 17 UIKit 0x00000001041feb8d __67- [UIStoryboardModalSegueTemplate newDefaultPerformHandlerForSegue:] _ block_invoke 243 18的UIKit 0x00000001041ec630 - [UIStoryboardSegueTemplate _performWithDestinationViewController:发送方:] 460 19的UIKit 0x00000001041ec433 - [UIStoryboardSegueTemplate _perform:] 82 20的UIKit 0x00000001041ec6f7 - [UIStoryboardSegueTemplate执行:] 156 21的UIKit 0x0000000103aa6a8d - [UIApplication的sendAction: to:from:forEvent:] 92 22 UIKit 0x0000000103c19e67 - [UIControl sendAction:to:forEvent:] 67 23 UIKit 0x0000000103c1a143 - [UIControl _sendActionsForEvents:withEvent:] 3 27 24的UIKit 0x0000000103c19263 - [UIControl touchesEnded:withEvent:方法] 601 25的UIKit 0x0000000103b1999f - [一个UIWindow _sendTouchesForEvent:] 835 26的UIKit 0x0000000103b1a6d4 - [一个UIWindow的SendEvent:] 865 27的UIKit 0x0000000103ac5dc6 - [UIApplication的的SendEvent:] 263 28的UIKit 0x0000000103a9f553 _UIApplicationHandleEventQueue 6660 29的CoreFoundation 0x0000000105095301 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION 17 30的CoreFoundation 0x000000010508b22c __CFRunLoopDoSources0 556 31的CoreFoundation 0x000000010508a6e3 __CFRunLoopRun 867 32的CoreFoundation 0x000000010508a0f8 CFRunLoopRunSpecific 488个33 GraphicsServices 0x000000010726dad2 GSEventRunModal 161 34的UIKit 0x0000000103aa4f09 UIApplicationMain 171 35 dhikr的0x0000000101f26282主114 36 libdyld.dylib 0x00000001064c392d启动1)的libc abi.dylib:与未捕获的异常终止NSException类型

我使用的代码是:

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

let foodList:[String] = ["Bread", "Meat", "Pizza", "Other"]
let drinkList:[String] = ["Water", "Soda", "Juice", "Other"]

@IBOutlet weak var mySegmentedControl: UISegmentedControl!
@IBOutlet weak var myTableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    var returnValue = 0

    switch(mySegmentedControl.selectedSegmentIndex) {
    case 0:
        returnValue = foodList.count
        break
    case 1:
        returnValue = drinkList.count
        break
    default:
        break
    }

    return returnValue
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let myCell = tableView.dequeueReusableCellWithIdentifier("myCells", forIndexPath: indexPath)

    switch(mySegmentedControl.selectedSegmentIndex) {
    case 0:
        myCell.textLabel!.text = foodList[indexPath.row]
        break
    case 1:
        myCell.textLabel!.text = drinkList[indexPath.row]
        break
    default:
        break
    }

    return myCell
}

@IBAction func segmentedControlActionChanged(sender: AnyObject) {
    myTableView.reloadData()  
}

这是 main.Storyboard

enter image description here

我多次检查代码,但它不起作用 . 首先我只使用 Table View ,看这个教程(https://www.youtube.com/watch?v=ABVLSF3Vqdg)我认为它可以在教程中使用 Segmented Control . 但仍然行不通 . 相同的代码,相同的错误 . 有人能帮我吗 ?

2 回答

  • 7

    您的故事板设置为期望一个名为 tableView 的插座,但实际插座名称为 myTableView .

    如果删除故事板中的连接并重新连接到正确的变量名称,则应解决问题 .

  • 78

    您是否有可能在某些时候将表视图的名称从“tableView”更改为“myTableView”?

相关问题