Access numberOfRowsInComponent from iCloud

My project has a UIPickerView and I need to return the count of an array for numberOfRowsInComponent.


Heres my code so far:

import UIKit
import CloudKit
class SecondViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource  {
    var db:CKDatabase?
    var currentRecords:[CKRecord] = []
    var currentRecordsInt:[Int] = []
    var numb: Int = 0
    var numbReturn: Int = 0
   
    @IBOutlet weak var pickCar: UIPickerView!
   
    override func viewDidLoad() {
        /
        self.pickCar.delegate = self
        self.pickCar.dataSource = self
        db = CKContainer.default().privateCloudDatabase
        super.viewDidLoad()
        /
    }
   
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    func getCarArrayNumb()   -> Int {
       
                let predicate = NSPredicate(value: true)
                let query = CKQuery(recordType: "mpgTracker", predicate: predicate)
       
        db?.perform(query, inZoneWith: nil, completionHandler: { (records:[CKRecord]?, e:Error?) in
            if e != nil {
                return
            }
            self.currentRecords = records!
            var record: CKRecord = records![0]
            print("getCarArrayNumb record.allKeys() \(record.allKeys())")   // OK, expected array
           
            var counts: [String: Int] = [:]
           
            var numb: Int = 0
           
            func countNumb() -> Int {
            for item in record.allKeys() {
                counts[item] = (counts[item] ?? 0) + 1
                numb = numb + 1
            }
                print("return numb: \(numb)")   //     13
                return numb
            }
            print("countNumb: \(countNumb())")  //     13
        })
        return 1
    }
  
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return getCarArrayNumb()
    }
   
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
       
        return ""
    }


I'm sure the answer is simple but so far it alludes me.


Thanks for any help!!


~paul

Replies

Hi Paul,


Try something like this :


Instead of a function call that returns the count, create a variable that is set in your getCarArrayNumb(). Reference the variable in your numberOfComponents(in:) UIPickerViewDataSource method as the return value. Once it is set, reload your UIPickerView :

https://developer.apple.com/documentation/uikit/uipickerview/1614380-reloadallcomponents


Hopefully this Helps.

Rico:

Thank you for your response. I've been trying to work it in but I'm not sure I understand. I actually return only 1 in numberOfComponents(in:) as that is how my app is designed. I am trying to get a return value for numberOfRowsInComponent, shouldn't I reference the variable in numberOfRowsInComponent?

And then, how do I use reloadAllComponents() for numberOfRowsInComponent? From what I can tell, reloadAllComponents() reloads the data in the components. Or am I missing something?

~paul

Issue description: After device restart icloud services became unresponsive. When i choose Settings - Account - iCloud, see message «Unable to load data» I cant sing out from Apple ID, got message « unable to connect to iCloud» i cant choose any option in Apple ID section ( Name, Phone Numbers, Email/ Password&Security/Payment,Shipping) - all inscriptions are gray and unresponsive Unable to load info about Family ( Family Sharing) Trusted devices are ineligible too Issue was tested onle with cellular data connection, unabe to test with Wi-Fi