UIPickerView - 在解包可选时意外发现了nil

时间:2016-04-07 06:41:04

标签: ios swift uitableview uipickerview

我有一个自定义单元格,可以显示numberPicker或timePicker。单元格将正确显示numberPicker,但每次尝试显示timePicker时都会崩溃。我放入了断点,在awakeFromNib中设置数据源后它崩溃了:

override func awakeFromNib() {
    super.awakeFromNib()

    if timePicker != nil {
        timePicker.delegate = self
        timePicker.dataSource = self <-- After this the crash occurs
    }

    if numberPicker != nil {
        numberPicker.delegate = self
        numberPicker.dataSource = self
    }
}

设置UIPickerView的完整代码如下:

@IBOutlet weak var timePicker: UIPickerView!
@IBOutlet weak var numberPicker: UIPickerView!

var times:[[Int]] = [[Int](0...59), [Int](0...59)]
var nums: [Int] = Array(0...100)

override func awakeFromNib() {
    super.awakeFromNib()

    if timePicker != nil {
        timePicker.delegate = self
        timePicker.dataSource = self
    }

    if numberPicker != nil {
        numberPicker.delegate = self
        numberPicker.dataSource = self
    }
}

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    if pickerView == numberPicker {
        return 1
    } else {
        return 2
    }
}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    if pickerView == numberPicker {
        return nums.count
    } else {
        return times[component].count
    }
}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    if pickerView == numberPicker {
        return String(nums[row])
    } else {
        return String(times[component][row])
    }
}

我已经尝试删除derivedData并多次清理我的构建,这也没有帮助。任何帮助表示赞赏。谢谢!

0 个答案:

没有答案