2016-06-21 85 views
0

我有一個功能完整的UIPickerView在UITableViewCell裏面,並且想完成兩件事情。自定義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

可能重複的[UIPickerView在UITableView](http://stackoverflow.com/questions/19646822/uipickerview-in-uitableview) – Sandy

回答

1

UITableView是UIScrollview的子類,UIPicker也是scrollview,你需要處理很多事情才能使它們一起工作。 首先,當你使用Picker在UITableview單元格上滾動時,哪一個是第一個響應?如果選擇器是第一個響應,並且選擇器的大小太大,比如設備屏幕大,則不能再滾動表格視圖。如果tableview是第一個響應,則不能滾動選取器,因爲表視圖將滾動。 我建議你添加Picker View作爲tableview/controller.view的子視圖,在你想添加Picker之前的單元格的位置/框架上,嘗試像TableView單元格內的Picker一樣僞造。

+0

嗨貼紙,感謝您的答覆。實際上,該應用程序已接近完成,並且只是試圖將其定製爲更好的外觀。目前的選取器視圖效果很好,只是沒有那麼吸引人。這就是問題的原因。謝謝 –