Swift - Table View在iPad上崩溃,而不是在iPhone上崩溃

时间:2016-08-31 01:00:57

标签: ios iphone swift uitableview ipad

拔掉我的头发。

我在iPhone模拟器上完全正常运行我的应用程序。但当我以为我完成了,我在iPad上运行它,我的桌面视图崩溃了。如果这是一个错误,或者我对一些简单的代码视而不见,请告诉我。

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 3
}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell: UITableViewCell!

    if indexPath.row < 3 {
        cell = tableView.dequeueReusableCellWithIdentifier("idCellSlider", forIndexPath: indexPath) as UITableViewCell

        let keyLabel = cell.contentView.viewWithTag(10) as? UILabel
        let valueLabel = cell.contentView.viewWithTag(20) as? UILabel
        let slider = cell.contentView.viewWithTag(30) as! CustomSlider

        var value: Float = 0.0


        switch indexPath.row {
        case 0:
            value = rate

            keyLabel?.text = "Rate"
            valueLabel?.text = NSString(format: "%.2f", rate) as String
            slider.minimumValue = AVSpeechUtteranceMinimumSpeechRate
            slider.maximumValue = AVSpeechUtteranceMaximumSpeechRate
            slider.addTarget(self, action: #selector(settingsViewController.handleSliderValueChange(_:)), forControlEvents: UIControlEvents.ValueChanged)

            slider.sliderIdentifier = 100

        case 1:
            value = pitch

            keyLabel?.text = "Pitch"
            valueLabel?.text = NSString(format: "%.2f", pitch) as String
            slider.minimumValue = 0.5
            slider.maximumValue = 2.0
            slider.addTarget(self, action: #selector(settingsViewController.handleSliderValueChange(_:)), forControlEvents: UIControlEvents.ValueChanged)

            slider.sliderIdentifier = 200


        default:
            value = volume

            keyLabel?.text = "Volume"
            valueLabel?.text = NSString(format: "%.2f", volume) as String
            slider.minimumValue = 0.0
            slider.maximumValue = 1.0
            slider.addTarget(self, action: #selector(settingsViewController.handleSliderValueChange(_:)), forControlEvents: UIControlEvents.ValueChanged)

            slider.sliderIdentifier = 300

        }

        if slider.value != value {
            slider.value = value
        }

    }else{

        cell = tableView.dequeueReusableCellWithIdentifier("idCellVoicePicker", forIndexPath: indexPath) as UITableViewCell

    }

    return cell
}

滑块值函数

func handleSliderValueChange(sender: CustomSlider) {

    switch sender.sliderIdentifier {
    case 100:
        rate = sender.value

    case 200:
        pitch = sender.value

    default:
        volume = sender.value
    }

    tbSettings.reloadData()

}

CustomSlider Class

class CustomSlider: UISlider {

    var sliderIdentifier: Int!

    override init(frame: CGRect) {
        super.init(frame: frame)
        sliderIdentifier = 0
    }


    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!

        sliderIdentifier = 0
    }
}

我认为这是某种错误,因为它再次在iPhone上完美运行,但在iPad上崩溃。

请帮忙!

更新:崩溃报告显示:&#34;致命错误:在展开“可选”值时意外发现nil&#34;

我不知道零值是多少?什么可以是零在iPhone上没有?

enter image description here

0 个答案:

没有答案