Uipickerview - Loop the Data

UIPickerView - Loop the data

There will be four steps here -- we'll set up some constants to hold the picker view data and a bit of configuration, then we'll add UIPickerViewDataSource and UIPickerViewDelegate methods, and we'll end with the viewDidLoad initialization.

First, the configuration:

private let pickerViewData = Array(0...59)     // contents will be 0, 1, 2, 3...59, change to whatever you want
private let pickerViewRows = 10_000 // any big number
private let pickerViewMiddle = ((pickerViewRows / pickerViewData.count) / 2) * pickerViewData.count

Note the pickerViewMiddle constant -- it's calculated to make it very easy to get our current value from the row offset. On to the data source -- we really only need to provide a title for each row, but we'll add a helper method to convert a row number to a value from the array:

extension ViewController : UIPickerViewDataSource {
func valueForRow(row: Int) -> Int {
// the rows repeat every `pickerViewData.count` items
return pickerViewData[row % pickerViewData.count]
}

func rowForValue(value: Int) -> Int? {
if let valueIndex = find(pickerViewData, value) {
return pickerViewMiddle + value
}
return nil
}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return "\(valueForRow(row))"
}
}

And finally we'll set up the delegate:

extension ViewController : UIPickerViewDelegate {
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerViewRows
}

// whenever the picker view comes to rest, we'll jump back to
// the row with the current value that is closest to the middle
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let newRow = pickerViewMiddle + (row % pickerViewData.count)
pickerView.selectRow(newRow, inComponent: 0, animated: false)
println("Resetting row to \(newRow)")
}

}

To initialize, in your viewDidLoad set the delegate and data source and then move to the correct row in the middle of your picker:

self.picker.delegate = self
self.picker.dataSource = self
let initialValue = 0
if let row = rowForValue(initialValue) {
self.picker.selectRow(row, inComponent: 0, animated: false)
}
// or if you just want to start in the middle:
// self.picker.selectRow(pickerViewMiddle, inComponent: 0, animated: false)

Can we enable looping of rows in a UIPickerView as we do it in UIDatePicker?

It is possible. For anyone of you who needs it,
try this.

Property for picker data won't retain result of for loop

You need to reload the pickerView inside the completion as it's asynchronous

for item in toyList {   
// ......
self.pickerData.append(pickerItem)
print(self.pickerData)
}
self.pickerView.reloadAllComponents()

How to load all data into picker view

I'm assuming your pickerView is set up properly but you are only seeing one row? If that's the case that's probably because of this line of code:

//change array to dictionary
guard let jsonDictionary:Dictionary = jsonArray.first else{
return
}//guard
print("jsonDictionary ==>\(jsonDictionary)")

You are only getting the first element of your array.

What I would do instead is just use jsonResponse directly like this:

var planDates = [Any]()
if let jsonResponse = jsonResponse {
for dictionary in jsonResponse {
planDates.append(dictionary["PlanDate"])
}
}

Then you can use planDates to populate your pickerView.

Or maybe, you are trying to load a pickerView with data from a dictionary?

First, your ViewController has to subclass UIPickerViewDataSource, UIPickerViewDelegate.

Then in ViewDidLoad, set your ViewController as the delegate/datasource for your UIPickerView:

repeatPicker.delegate = self
repeatPicker.dataSource = self

Then implement your Delegate/Datasource methods:

   // The number of components of your UIPickerView
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}

// The number of rows of data
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
// Return number of planDate entries
}

// The data to return for the row and component (column) that's being passed in
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

}


Related Topics



Leave a reply



Submit