我有一個功能完整的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)
{
}
}
更新: 我不問如何創建一個選擇器視圖,如上面的代碼的偉大工程。我只問如何定製它,使其更具吸引力。
可能重複的[UIPickerView在UITableView](http://stackoverflow.com/questions/19646822/uipickerview-in-uitableview) – Sandy