如何修复错误:此类与键 tableView 的键值编码不兼容。 [复制]

2024-02-18

我做了一个应用程序Table View and Segmented Control,这是我第一次。我正在使用一些代码和一些教程,但它不起作用。当我运行我的应用程序时,它崩溃了,并在日志中显示此错误:

MyApplication[4928:336085] * 由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[ setValue:forUndefinedKey:]:此类与键 tableView 的键值编码不兼容。” * 首先抛出调用堆栈: ( 0 核心基础 0x000000010516fd85 __exceptionPreprocess + 165 1 libobjc.A.dylib 0x0000000105504deb objc_异常_抛出 + 48 2 CoreFoundation 0x000000010516f9c9 -[NSException 引发] + 9 3 基础 0x000000010364e19b -[NSObject(NSKeyValueCoding) setValue:forKey:] + 288 4 UIKit 0x0000000103c37d0c -[UIViewController setValue:forKey:] + 88 5 UIKit 0x0000000103e6e7fb -[UIRuntimeOutletConnection 连接] + 109 6 CoreFoundation 0x00000001050a9890 -[NSArray makeObjectsPerformSelector:] + 224 7 UIKit 0x0000000103e6d1de -[UINib instantiateWithOwner:选项:] + 1864 8 UIKit 0x0000000103c3e8d6 -[UIViewController _loadViewFromNibNamed:捆绑:] + 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:完成:] + 4002 15 UIKit 0x0000000103c5585c -[UIViewController _performCooperativePresentOrDismiss:动画:] + 489 16 UIKit 0x0000000103c5536b -[UIViewController PresentViewController:动画:完成:] + 179 17 UIKit 0x00000001041feb8d __67-[UIStoryboardModalSegueTemplate newDefaultPerformHandlerForSegue:]_block_invoke + 243 18 UIKit 0x00000001041ec630 -[UIStoryboardSegueTemplate _performWithDestinationViewController:发送者:] + 460 19 UIKit 0x00000001041ec433-[UIStoryboardSegueTemplate_执行:] + 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:] + 327 24 UIKit 0x0000000103c19263 -[UIControl TouchsEnded:withEvent:] + 601 25 UIKit 0x0000000103b1999f - [UIWindow _sendTouchesForEvent:] + 835 26 UIKit 0x0000000103b1a6d4 - [UIWindow发送事件:] + 865 27 UIKit 0x0000000103ac5dc6 - [UI应用程序发送事件:] + 263 28 UIKit 0x0000000103a9f553 _UIApplicationHandleEventQueue + 6660 29 核心基础 0x0000000105095301 _CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION_ + 17 30 核心基础 0x000000010508b22c __CFRunLoopDoSources0 + 556 31 核心基础 0x000000010508a6e3 __CFRunLoopRun + 867 32 核心基础 0x000000010508a0f8 CFRunLoopRunSpecific + 488 33 图形服务 0x000000010726dad2 GSEventRunModal + 161 34 UIKit 0x0000000103aa4f09 UIApplicationMain + 171 35 迪克尔 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()  
}

Here is 主故事板

我检查了很多次代码,但它不起作用。首先我只能使用Table View,观看本教程(https://www.youtube.com/watch?v=ABVLSF3Vqdg https://www.youtube.com/watch?v=ABVLSF3Vqdg)我认为使用它会起作用Segmented Control如教程中所示。但还是不行。相同的代码,相同的错误。 有人能帮我吗 ?


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

如果删除情节提要中的连接并重新连接到正确的变量名称,应该可以解决问题。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何修复错误:此类与键 tableView 的键值编码不兼容。 [复制] 的相关文章

随机推荐