在uitableviewcell里面定制uipickerview

时间:2016-06-21 04:21:09

标签: ios swift uitableview uipickerview

我在UITableViewCell中有一个功能齐全的UIPickerView,想完成两件事。

UIPickerView显示4个组件,但第一个组件很少被引用。因此,我想要A(首选)让它显示空白为0并滚动到1,或B修改颜色,使0不是那么占优势。

其次,我想隐藏UIPickerView直到被选中。

我找到了两个选项,但都位于UIViewController中,并且在UITableViewCell中不起作用。

import UIKit

class DiveDurationTableViewCell: UITableViewCell, UIPickerViewDelegate, UIPickerViewDataSource

{
    @IBOutlet var textfieldBizCat: UITextField!
    @IBOutlet var pickerBizCat: UIPickerView! = UIPickerView()


    // ---------------------------------------------------------------------------------------------
    // MARK: - Internal Constant

    let numberOfComponents: Int             = 4
    let FirstDigitComponentRows: Int        = 2
    let SecondDigitComponentRows: Int       = 10
    let ThirdDigitComponentRows: Int        = 10
    let DisplayMinuteComponentRows: Int     = 1

    let Minutes: String                     = "Min"
    let MinNumber                           = 0
    let MaxNumber                           = 9


    private var diveTime: Array<Int>        = [Int]()

    var FirstDigit: Int                     = 0
    var SecondDigit: Int                    = 0
    var ThirdDigit: Int                     = 0
    var DisplayMinutes: String              = "Min"


    // ---------------------------------------------------------------------------------------------
    // MARK: - IBOutlets

    @IBOutlet var diveTimePicker: UIPickerView!


    // ---------------------------------------------------------------------------------------------
    // MARK: - UITableVieCell Methods


    //
    //  Called when we are initialized from a storyboard or nib file.
    //
    override func awakeFromNib()
    {
        super.awakeFromNib()


        //
        //  We create an array of integers from 0 to 9.
        //

        for i in self.MinNumber ..< self.MaxNumber+1
        {
            self.diveTime.append(i)
        }

        self.diveTimePicker.selectRow(0, inComponent: 0, animated: true)
        self.diveTimePicker.selectRow(0, inComponent: 1, animated: true)
        self.diveTimePicker.selectRow(0, inComponent: 2, animated: true)

    }

    //
    //  The item has been selected in the table.
    //

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



    // ---------------------------------------------------------------------------------------------
    // MARK: - UIPickerViewDataSource Methods


    //
    //  Number of components in the picker.
    //

    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int
    {
        return self.numberOfComponents
    }

    //
    //  Number of rows in each component.
    //
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
    {
        if component == 0
        {
            return self.FirstDigitComponentRows
        } else if component == 1   {
            return self.SecondDigitComponentRows
        } else if component == 2   {
            return self.ThirdDigitComponentRows
        } else  {
            return self.DisplayMinuteComponentRows
        }
    }

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
    {

        if component == 0
        {
            return String(self.diveTime[row])
        }
        if component == 1
        {
            return String(self.diveTime[row])
        }
        if component == 2
        {
            return String(self.diveTime[row])
        }

        else
        {
         return self.DisplayMinutes
            }
        }


    //
    //  The user has selected in item in the picker view.
    //
    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
    {
    }
}

更新: 我不是在问如何创建一个选择器视图,因为上面的代码工作得很好。我只是问如何定制它以使其更具吸引力。

1 个答案:

答案 0 :(得分:1)

UITableView是UIScrollview的子类,UIPicker也是scrollview,你需要处理很多东西才能让它们一起工作。 第一件事是当您使用Picker在UITableview单元格上滚动时,哪一个是第一个响应?如果Picker是第一个响应并且拾取器的大小太大(例如设备屏幕大),则您将无法再滚动表视图。如果tableview是第一个响应,则无法滚动Picker,因为表视图将滚动。 我建议你将Picker View作为tableview / controller.view的子视图添加到你想要添加Picker的单元格的位置/框架之前,尝试假装像tableview单元格中的Picker一样。