在TableView中实现多个PickerView

时间:2016-05-04 06:16:54

标签: ios swift uitableview uipickerview

我尝试在UIPickerView内部实施多个UITableView,每一行都包含一个独立的UIPickerView。问题是,当我在一个PickerView中更改值时,它会自动在其他行上更改,但有时不会。但/ p>

但我希望每个UIPickerView都有独立的UITableViewCell

enter image description here enter image description here

enter image description here

自定义单元格

    class PrayerCell: UITableViewCell, UIPickerViewDataSource, UIPickerViewDelegate {

    @IBOutlet var detailView: UIView!
    @IBOutlet var name: UILabel!
    @IBOutlet var time: UILabel!
    @IBOutlet var pickerView: UIPickerView!
    @IBOutlet var toggleAlarm: UISwitch!

    var isObserving = false;


    class var expandedHeight: CGFloat {
        get {
            return 200
        }
    }

    class var defaultHeight: CGFloat {
        get {
            return 44
        }
    }

    func checkHeight() {
        detailView.hidden = (frame.size.height < PrayerCell.expandedHeight)
    }
    override func awakeFromNib() {
        super.awakeFromNib()

        self.pickerView.delegate = self
        self.pickerView.dataSource = self
    }

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

    func watchFrameChanges() {
        if !isObserving {
            addObserver(self, forKeyPath: "frame", options: [.New, .Initial], context: nil)
            isObserving = true;
        }
    }

    func ignoreFrameChanges() {
        if isObserving {
            removeObserver(self, forKeyPath: "frame")
            isObserving = false;
        }
    }

    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        if keyPath == "frame" {
            checkHeight()
        }
    }

    let pickerData = [15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return pickerData.count
    }

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return String(pickerData[row])
    }

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        print(row)
        updateLabel()
    }

    func updateLabel(){
        // let value = pickerData[pickerView.selectedRowInComponent(0)]
        // minsBeforeLabel.text = String(value) + " Minutes Before "
    }
}

1 个答案:

答案 0 :(得分:0)

使用以下所需的开源实现。

Bohr