Théo Fleismaher Théo Fleismaher - 1 year ago 90
iOS Question

Fatal error: unexpectedly found nil while unwrapping an Optional value (swipe nav)

I know the question have already been asked many times but I checked all the possible issues in the various answers and it still doesn't work. I've created a swipe navigation between ViewControllers and added a TableView in a ViewController, which worked perfectly. Now that I try to customize the TableView, this error message appears and the app crashes.

Here is the code of the ViewController where I added the TableView :

import UIKit

class VC0: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var myTableView: UITableView!

var names = ["Artist name", "Artist Name", "Artist name", "Artist name", "Artist name", "Artist name"]
var cities = ["City", "City", "City", "City", "City", "City"]
var images = [UIImage(named: "avatar"), UIImage(named: "avatar1"), UIImage(named: "avatar2"), UIImage(named: "avatar3"), UIImage(named: "avatar4"), UIImage(named: "avatar5")]

override func viewDidLoad() {

self.myTableView.registerClass(CustomCell.self, forCellReuseIdentifier: "cell")
self.myTableView.dataSource = self

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return names.count

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.myTableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomCell = images[indexPath.row] = names[indexPath.row] = cities[indexPath.row]

return cell


The thread appears on this line : = images[indexPath.row]

Here is the project if you want to take a look at the full code :

Answer Source

Your use of registerClass is overwriting your creation of the prototype cell in your storyboard. From the UITableView documentation:

If you previously registered a class or nib file with the same reuse identifier, the class you specify in the cellClass parameter replaces the old entry.

Instead, you're creating new, blank, instances of CustomCell, which doesn't have your outlets hooked up. Remove that line, and things should work.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download