我正在创建时钟pickerView并使用了以下内容:
var countSec = Array(0...10)
var countMin = Array(0...59)
var countHour = Array(0...59)
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if component == 0{
for count in countSec {
return "\(count)"
}
}else{
for count in countMin{
return "\(count)"
}
}
return nil
}
但是当我运行时,所有数据均为0。
请勿在titleForRow
中使用循环。只需为请求的行返回适当的值即可。
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if component == 0 {
return "\(countSec[row])"
} else {
return "\(countMin[row])"
}
}