0
votes

I am attempting to learn how to use the UITableView element, however despite watching a simple tutorial and copying the code character for character, the table view still crashes the application despite not raising any errors in the editor.

Code:

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
}

}

Error:

2015-11-05 13:51:49.120 collaboration[17405:218899] * Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key myTableView.' * First throw call stack: ( 0 CoreFoundation 0x000000010ac16f45 exceptionPreprocess + 165 1 libobjc.A.dylib
0x000000010c93adeb objc_exception_throw + 48 2 CoreFoundation
0x000000010ac16b89 -[NSException raise] + 9 3 Foundation
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 view] + 27 12 UIKit 0x000000010bd687e7 -[_UIFullscreenPresentationController _setPresentedViewController:] + 87 13 UIKit 0x000000010b595dde -[UIPresentationController initWithPresentedViewController:presentingViewController:] + 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 perform:] + 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 collaboration
0x000000010aa22f6d main + 109 37 libdyld.dylib
0x000000010d44292d start + 1 38 ???
0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

Thanks

2
Did you do something in storyboard with your ViewController, add UITableView and custom cells?Alexey Pichukov
What? Sorry, I do not understand what you mean...Nick Pitoniak
Somthing in your code is trying to access myTableView. Which does not exist. Did you maybe rename the table view property?joern
@Nick, Check datasource and delgate connection of your tableview and add below code in your viewdidload() : Your_tableName.dataSorce=self;Your_tableName.delegate=self;Manikandan D

2 Answers

1
votes

Go to storyboard, right click on your tableView and check whether it's outlet is connected to name "myTableView" or not. If yes, then disconnect it using the cross icon. I guess your tableView is having two outlets, one named "myTableView" and other "tableView". Make sure you keep the name the one in your code.

0
votes

First Thing first:

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

Then connect TableView Delegate to self in viewDidLoad()

tableView.delegate = self

And Also make sure that you gave the cell name is the same as homeCell