Reputation: 1625
In my use case I would like to add in numbers consisting of 4 digits, like in the image. The first digit should be 0 or 1.
My code so far is:
func numberOfComponents(in weightPickerView: UIPickerView) -> Int {
return 4
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return loopingMargin * numbers.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return numbers[row % numbers.count]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
digits[component] = row % numbers.count
let weightString = "\(digits[0])\(digits[1])\(digits[2])\(digits[3])"
weightField.text = weightString
}
How can I achieve that?
Upvotes: 0
Views: 1319
Reputation: 2349
Considering:
let numbers = [2,10,10,10]
var digits = [0,0,0,0]
Update your delegate methods like this:
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return numbers[component]
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return String(row)
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
digits[component] = row
let weightString = "\(digits[0])\(digits[1])\(digits[2])\(digits[3])"
weightField.text = weightString
}
Upvotes: 3
Reputation: 100503
You can try
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if(component == 0)
{
return 2
}
else
{
return loopingMargin * numbers.count
}
}
Upvotes: 1