0

I am trying to implement a horizontal picker view (AKPickerView-Swift from GitHub) inside a custom table view cell.

The picker view is a custom class, and in the prototype cell view in the storyboard, I added two UIViews that are of that class, and wired them as IBOutlet to my custom UITableViewCell class.

When I run the program, it crashes with the error:

EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

at the line in the custom UITableViewCell class that says self.eventTypePicker.delegate = self (see image).

On the right hand side of that screenshot, you can see an implementation of the same picker view in a regular view controller, which works fine.

The output window at the bottom of the screen in XCode shows the following error:

fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

What should I do differently when I am trying to implement these picker views in a table view cell instead of a regular view controller?

I am using XCode 7.1 and writing in Swift.

Here's the code in my WorkoutTableViewCell.swift class

import UIKit

class WorkoutTableViewCell: UITableViewCell, AKPickerViewDataSource, AKPickerViewDelegate {

@IBOutlet weak var eventSelectorLabel: UILabel!
@IBOutlet weak var repsSelectorLabel: UILabel!
@IBOutlet weak var eventTypePicker: AKPickerView!
@IBOutlet weak var repsPicker: AKPickerView!

let distance = ["100m", "100m h", "200m", "200m h", "400m", "400m h"]
let sets = ["-", "1", "2", "3", "4", "5", "6", "7"]

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state

    self.eventTypePicker.delegate = self
    self.eventTypePicker.dataSource = self
    self.eventTypePicker.font = UIFont(name: "HelveticaNeue-Light", size: 10)!
    self.eventTypePicker.highlightedFont = UIFont(name: "HelveticaNeue", size: 10)!
    self.eventTypePicker.pickerViewStyle = .Wheel
    self.eventTypePicker.maskDisabled = false
    self.eventTypePicker.reloadData()

    self.repsPicker.delegate = self
    self.repsPicker.dataSource = self
    self.repsPicker.font = UIFont(name: "HelveticaNeue-Light", size: 10)!
    self.repsPicker.highlightedFont = UIFont(name: "HelveticaNeue", size: 10)!
    self.repsPicker.pickerViewStyle = .Wheel
    self.repsPicker.maskDisabled = false
    self.repsPicker.reloadData()

    eventTypePicker.tag = 0
    repsPicker.tag = 1
}

// MARK: - AKPickerViewDataSource

func numberOfItemsInPickerView(effortPicker: AKPickerView) -> Int {
    if effortPicker.tag == 0{

        return self.sets.count
    } else if effortPicker.tag == 1 {
        return self.distance.count
    }
    return 1
}

func pickerView(effortPicker: AKPickerView, titleForItem item: Int) -> String {

    if effortPicker.tag == 0{

        return self.sets[item]
    } else if effortPicker.tag == 1 {
        return self.distance[item]
    }

    return ""
}

func effortPicker(effortPicker: AKPickerView, imageForItem item: Int) -> UIImage {
    return UIImage(named: self.distance[item])!
}

func scrollViewDidScroll(scrollView: UIScrollView) {
    // println("\(scrollView.contentOffset.x)")
}

}
Lorenzo
  • 3,293
  • 4
  • 29
  • 56
hjouh1
  • 35
  • 1
  • 8
  • Pleas can you post the code – Victor Sigler Nov 15 '15 at 21:19
  • fatal error: unexpectedly found nil while unwrapping an Optional value (lldb) – hjouh1 Nov 15 '15 at 21:26
  • It looks like the picker views don't get populated and the app crashes when they are nil https://www.dropbox.com/s/8exzvlx1yrjwqbe/Screenshot%202015-11-15%2016.31.44.png?dl=0 – hjouh1 Nov 15 '15 at 21:32
  • Now also posted the code in the table view cell class – hjouh1 Nov 15 '15 at 21:40
  • Possible duplicate of [What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?](http://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu) – jtbandes Nov 16 '15 at 00:12
  • @jtbandes looks like there's similarities to that issue, but even if I add "?" after the object names (e.g., self.eventTypePicker?.delegate = self), the pickers still down't work properly. The app doesn't crash, but none of the picker views show up on the screen. – hjouh1 Nov 16 '15 at 02:46

0 Answers0